gpt4 book ai didi

android - 在 nestedscrollview 中使用时的分页库加载所有数据

转载 作者:行者123 更新时间:2023-11-30 05:06:13 30 4
gpt4 key购买 nike

我正在使用分页库加载数据并填充放置在 nestedscrollview 中的 recyclerview。但就像,分页会自动工作,直到从 API 获取所有数据。我知道这是因为 nestedscrollview。但不幸的是,我的布局需要 ScrollView ,因为我在这个 fragment 中有一个除了 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>

当我不使用 nestedscrollview 时一切正常。 googlesamples git repo regsrd 中有一个 Unresolved 问题。

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

有没有人知道当 recyclerview 位于带有来自 Android jetpack 的分页库的 ScrollView 中时,我们如何实现分页。我知道我们可以实现传统类型的分页,将监听器附加到 nestedscrollview,但我希望使用体系结构组件库实现分页。 https://developer.android.com/topic/libraries/architecture/paging/

最佳答案

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

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

注意:确保您已为 CoordinatorLayout 和 AppBarLayout 指定了 id,以便它在返回堆栈上保留滚动位置。

关于android - 在 nestedscrollview 中使用时的分页库加载所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54547780/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com