分页库在嵌套滚动视图中使用时会加载所有数据。

13

我正在使用分页库来加载数据并填充我的recyclerview,它位于nestedscrollview中。但是问题在于,分页自动工作直到从API获取所有数据为止。我知道这是由于nestedscrollview引起的。但不幸的是,我的布局需要scrollview,因为在此片段中除了recyclerview还有一个顶部部分。

这是我的布局:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
   >
   <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

   //have a layout here which scrolls with recyclerview

      <Recyclerview />

   </ConstraintLayout>

</NestedScrollView>

如果不使用嵌套滚动视图,一切都正常。在googlesamples git仓库中有一个关于这个问题的未解决问题。

https://github.com/googlesamples/android-architecture-components/issues/215

如果RecyclerView在带有Android Jetpack分页库的ScrollView中,有没有人有想法如何实现分页?我知道我们可以使用传统类型的分页将侦听器附加到NestedScrollView上,但我正在寻找使用Architecture Component库实现分页的方法。 https://developer.android.com/topic/libraries/architecture/paging/

4个回答

9

使用下面的代码可以解决这个问题。以下是视图层次结构:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar_layout"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false">

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

                <!-- Put here elements that you need above the recycler view -->

            </LinearLayout>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>
    <!-- RecyclerView -->
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:scrollbars="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

注意:确保您已将id分配给CoordinatorLayout和AppBarLayout,以便在后退堆栈上保留滚动位置。

1
问题是嵌套滚动视图中的RecyclerView。分页库与此无关。如果您尝试在RecyclerView的滚动侦听器上加载数据,则行为将相同。

1
你有解决方案吗? - Nanda Z

0
你可以考虑使用ConcatAdapter或者给你的PagingData添加一个header。这样可以避免NestedScrollView的冗余。

0

分页库与嵌套滚动视图不兼容。因此,您必须将NesteScrollView更改为带有android:nestedScrollingEnabled="true"的ScroolView,类似于以下内容:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:nestedScrollingEnabled="true"
   >
   <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

   //have a layout here which scrolls with recyclerview

      <Recyclerview />

   </ConstraintLayout>

</ScrollView>

如果RecyclerView的父布局不是SwipeRefreshLayout,这样做可以解决分页问题,但滚动行为会变得奇怪。 - undefined

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