【问题标题】:disable touch events for RecyclerView inside NestedScrollV禁用 NestedScrollV 内 RecyclerView 的触摸事件
【发布时间】:2016-01-07 16:56:27
【问题描述】:

我的布局如下:

    <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 的折叠/展开行为?

【问题讨论】:

  • 如果我记得...您可能必须实际创建自己的自定义 recyclerview 适配器并让它停止检测滚动

标签: android scroll android-recyclerview android-collapsingtoolbarlayout


【解决方案1】:

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

android:nestedScrollingEnabled="false"

问题消失了,请尝试添加它并告诉我它是否有效

祝你好运

【讨论】:

  • 我想这可能有效,但我已经采取了其他方式。我用所需数量的 textviews 替换了我的 recyclerview ..))我会检查这个作为答案。
【解决方案2】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    相关资源
    最近更新 更多