NestedScrollView嵌套在NestedScrollView中无法滚动。

3

我知道这个问题已经被提出很多次了,但我几乎尝试了所有的解决方案,甚至那个所谓的神奇解决方案对我也没有用!(父级是NestedScrollview,在其子项中还有另一个NestedScrollview

    parent.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            Log.v(TAG,"PARENT TOUCH");
            child.getParent().requestDisallowInterceptTouchEvent(false);
            return false;
        }
    });

    child.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event)
        {
            Log.v(TAG,"CHILD TOUCH");
            // Disallow the touch request for parent scroll on touch of child view
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

在Logcat中,我得到了“PARENT TOUCH”和“CHILD TOUCH”,但是子项无法滚动!奇怪的是,这个解决方案对其他人有效,但对我无效。
这是我的XML结构:(PS:我很想尝试你的解决方案)(编辑后)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
       <NestedScrollView
            android:layout_width="match_parent"
            android:id="@+id/parentscrollview"
            android:layout_height="wrap_content">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_marginTop="20dp"
                android:textSize="15sp"
                android:layout_height="wrap_content"
                android:text="txt" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="txt2"
                android:textSize="15sp"
                android:layout_below="@+id/textView"
                android:id="@+id/textView2"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text=""
                android:textSize="15sp"
                android:layout_below="@+id/textView2"
                android:id="@+id/textView4"
                />


            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_below="@+id/textView4"
                android:id="@+id/autocainatiner"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="6dp"
                    android:textSize="15sp"
                    android:layout_marginTop="20dp"
                    android:textAllCaps="true"
                    android:text="Please Add/chose the observed clinical features :"
                    />

                <com.mycardboarddreams.autocompletebubbletext.MultiSelectEditText
                    android:id="@+id/auto_text_complete"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:textStyle="bold"
                    android:padding="5dp"
                    android:background="#FFEEEEEE"/>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">


                <Button
                    android:id="@+id/btnPlanet"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:onClick="blahblah"
                    android:text="Enter" />
                </RelativeLayout>


            </LinearLayout>

            <NestedScrollView
                android:layout_width="match_parent"
                android:id="@+id/scrollframe"
                android:layout_below="@+id/autocainatiner"
                android:fillViewport="true"
                android:layout_height="200dp">
                <FrameLayout
                    android:id="@+id/auto_list_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimaryDark"/>
            </NestedScrollView>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/scrollframe"
                android:orientation="vertical"
                >
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="40dp"
                    android:textSize="16sp"
                    android:textAllCaps="true"
                    android:textStyle="bold"
                    android:gravity="center_horizontal"
                    android:text="Suggested Diganosi list (Please Click on suggested diseases for more infos)"
                    android:id="@+id/textView3"
                    />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:text="Suggested Diganosi list (Please Click on suggested diseases for more infos)"
                    android:layout_marginTop="25dp"
                    android:textAllCaps="true"
                    android:id="@+id/diagnosis"
                    />

                <FrameLayout
                    android:id="@+id/auto_list_container2"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:visibility="gone"
                    android:background="@color/colorPrimaryDark"/>

            </LinearLayout>


            </RelativeLayout>
        </NestedScrollView>

    </RelativeLayout>
1个回答

1
通过创建以下内容解决了它:

public class NoInterceptScrollView extends ScrollView {

    public NoInterceptScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }

}

我希望您能将其用作父类,并将两个嵌套滚动视图更改为滚动视图+删除禁用父触摸的代码...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接