简体   繁体   English

在NestedScrollV中禁用RecyclerView的触摸事件

[英]disable touch events for RecyclerView inside NestedScrollV

I have a layout like below: 我的布局如下:

    <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>

Where idea is toolbar is collapsing when scrolled up (down?). 向上滚动(向下滚动)时工具栏崩溃的地方。 But there is a problem with where touch event is occurred. 但是发生触摸事件的地方存在问题。 If scroll initiated on any element except RecyclerView, everything works like a charm. 如果在除RecyclerView之外的任何元素上启动了滚动,则所有内容都将像超级按钮一样工作。 But if scroll touch is initiated on recyclerview, NestedScrollView starts scrolling without toolbar collapse or expand. 但是,如果在recyclerview上启动了滚动触摸,则NestedScrollView将开始滚动而不会折叠或展开工具栏。 RecyclerViews use custom layout manager, which wraps its content (each RecyclerView has few items). RecyclerViews使用自定义布局管理器,该管理器包装其内容(每个RecyclerView都有几项)。 So here is a question. 所以这是一个问题。 How to tell RecyclerView to not to interfere with NestedScrollView's collapsing/expanding behavior? 如何告诉RecyclerView不要干扰NestedScrollView的折叠/展开行为?

i had the same problem but with NestedScrollView i used 我有同样的问题,但我使用的是NestedScrollView

android:nestedScrollingEnabled="false"

and the problem was gone, please try to add this and tell me if it works 并且问题消失了,请尝试添加它并告诉我是否可行

Good luck 祝好运

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM