【问题标题】:RecyclerView scroll conflicts with ScrollViewRecyclerView 滚动与 ScrollView 冲突
【发布时间】:2017-03-27 19:34:14
【问题描述】:

我正在使用类似于Google Play app 的布局结构。但就我而言,我已经修复了水平的RecyclerView 容器,所以我更喜欢使用NestedScrollView 作为根视图。这是我的布局结构

<NestedScrollView>

    <HeaderView />
    <RecyclerView />

    <HeaderView />
    <RecyclerView />

    <HeaderView />
    <RecyclerView />

</NestedScrollView>

问题在于,当我垂直滚动 NestedScrollView 并且它一甩时,我无法立即停止它并开始滚动 RecyclerView,我需要等待一分钟直到滚动停止,如果它在屏幕上,请移开手指,然后尝试滚动RecyclerView,当RecyclerView 滚动时反之亦然。如何消除滚动冲突以使我的布局滚动流畅,就像在 Google Play 应用程序中一样?

【问题讨论】:

标签: android


【解决方案1】:

我还设计了一个类似于 GooglePlay 应用的应用。只需按照以下步骤:

  1. 将属性app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到android.support.v4.widget.NestedScrollView
  2. 使用RelativeLayout 作为android.support.v4.widget.NestedScrollView 的直接子级并将属性android:descendantFocusability="blocksDescendants" 添加到RelativeLayout
  3. RelativeLayout内部,添加一个垂直的LinearLayout作为所有Header view(TextView)和RecyclerView的容器

这是工作代码的结构。试试这个:

<android.support.design.widget.CoordinatorLayout>

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

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:descendantFocusability="blocksDescendants">

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

                <!-- Header view -->
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:paddingLeft="16dp"
                    android:gravity="center_vertical"
                    android:textSize="14sp"
                    android:textStyle="bold"
                    android:fontFamily="sans-serif"
                    android:textColor="@color/textColorSecondary"
                    android:text="Header"/>

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

                <!-- Header view -->
                <TextView />

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

                <!-- Header view -->
                <TextView />

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

                <!-- Header view -->
                <TextView />

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

                <!-- Header view -->
                <TextView />

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

                ................
                ..........................    
            </LinearLayout>
        </RelativeLayout>

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

希望对你有帮助~

【讨论】:

  • 你知道 appbar_scrolling_view_behavior 的用途是什么吗?为什么大家都推荐我使用它?我没有写我有一个应用栏,或者这个布局可能是片段布局,容器已经有这个属性
  • @Near1999:AppBarLayout.ScrollingViewBehavior 用于在此特定视图上发生滚动事件时通知 AppBarLayout。行为必须建立在触发事件的视图上。
  • 例如:当 CoordinatorLayout 注意到 RecyclerView 中声明的该属性时,它将在其中包含的其他视图中搜索与该行为关联的任何相关视图。在这种特殊情况下,AppBarLayout.ScrollingViewBehavior 描述了 RecyclerView 和 AppBarLayout 之间的依赖关系。 RecyclerView 的任何滚动事件都应触发对 AppBarLayout 布局或其中包含的任何视图的更改。
  • @Near1999 如果您的视图已经具有此属性,请忽略。申请 2 和 3 号。脚步。我刚刚添加了这个,因为我不知道你已经应用了这个属性。谢谢~
  • 这并不能解决滚动冲突的问题
猜你喜欢
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多