在 NestedScrollV 中禁用 RecyclerView 的触摸事件

disable touch events for RecyclerView inside NestedScrollV

我的布局如下:

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="0dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:elevation="0dp"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax">

                <ImageView
                    android:id="@+id/backdrop"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/devito" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/rating"
                    android:fitsSystemWindows="true"
                    android:padding="8dp"
                    android:text="Danny DeVitto"
                    android:textSize="24sp" />

            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:elevation="0dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Info"
                android:textAppearance="@style/TextAppearance.AppCompat.Title" />

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/card_margin">

                <LinearLayout
                    style="@style/Widget.CardContent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.v7.widget.RecyclerView
                    android:id="@+id/someList"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/cheese_ipsum" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/card_margin"
                android:layout_marginLeft="@dimen/card_margin"
                android:layout_marginRight="@dimen/card_margin">

                <LinearLayout
                    style="@style/Widget.CardContent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Friends"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                    <android.support.v7.widget.RecyclerView
                    android:id="@+id/anotherList"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

        </LinearLayout>


    </android.support.v4.widget.NestedScrollView>


</android.support.design.widget.CoordinatorLayout>

当向上(向下)滚动时,工具栏会折叠起来。但是发生触摸事件的位置存在问题。如果在除 RecyclerView 之外的任何元素上启动滚动,一切都会像魅力一样运作。但是,如果在 recyclerview 上启动滚动触摸,NestedScrollView 将开始滚动,而不会折叠或展开工具栏。 RecyclerViews 使用自定义布局管理器,它包装了它的内容(每个 RecyclerView 都有很少的项目)。所以这是一个问题。如何告诉 RecyclerView 不要干扰 NestedScrollView 的 collapsing/expanding 行为?

我遇到了同样的问题,但是我使用了 NestedScrollView

android:nestedScrollingEnabled="false"

问题解决了,请尝试添加这个并告诉我它是否有效

祝你好运

试试这个:

yourRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener()  {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            return true; //Consume the touch event
        }
        ...
    });