NestedScrollView与RecyclerView的滚动

11

我在一个NestedScrollView中布局了一个RecyclerView,希望能够滑动NestedScrollView时也跟着滑动RecyclerView,但只有当RecyclerView滑动到底部时才会出现这种情况,以下是我的布局代码:

<android.support.v4.widget.NestedScrollView
android:id="@+id/lists_frame"
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:animateLayoutChanges="true"
tools:context="com.example.niuky.design.MainActivity4"
>
    <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   >
    <View
       android:id="@+id/header"
       android:layout_width="match_parent"
       android:layout_height="256dp"
       android:background="@color/material_blue_grey_800"
       />

   <View
       android:id="@+id/tabs"
       android:layout_width="match_parent"
       android:layout_height="?android:attr/actionBarSize"
       android:background="@color/material_blue_grey_950"
       />

  <android.support.v7.widget.RecyclerView
      android:id="@+id/recyclerview2"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:minHeight="700dp"

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

这是我的运行时视图:NestedScrollView中的RecyclerView


为什么您不使用带有AppBarLayout和NestedScrollView的支持CoordinatorLayout呢? - tato.rodrigo
@tato.rodrigo 首先我使用了 CoordinatorLayoutAppBarLayout,但是我没有得到我想要的结果。你有什么想法吗?非常感谢! - Kevin
@tato.rodrigo 如果我们不想使用设计支持库怎么办?NestedScrollView应该允许我们这样做...但是,就像谷歌最近的做法一样,根本没有文档。 - mato
@acntwww 你好,我目前也遇到了同样的问题,你已经找到解决方法了吗? :) - edwinj
2个回答

2
<android.support.v4.widget.NestedScrollView
    android:id="@+id/nested_scrollbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:scrollbars="none" >
        <LinearLayout
            android:id="@+id/nested_scrollbar_linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

                <android.support.v7.widget.CardView
                    android:id="@+id/flexible.example.cardview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

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

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/list_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>

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

.setNestedScrollingEnabled 应用于 recyclerview 并将其设置为 false

附:对于 API 低于 21 的情况:

ViewCompat.setNestedScrollingEnabled(recyclerView, false);


1

在 RecyclerView 上加入 android:nestedScrollingEnabled="false" 属性


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