我正在使用带有两个垂直方向的片段的 ViewPager2。当用户向下滑动到第二个片段时,有一个 RecyclerView 在同一垂直方向滚动内容。
问题是当我滚动 RecyclerView 的内容时,有时 ViewPager2 捕获滚动事件,有时 RecyclerView 捕获滚动事件。
我希望这样,当用户滚动到 RecyclerView 的顶部时,ViewPager 仅在用户到达 RecyclerView 内容的顶部时才滑回第一个片段。
我试过使用 recyclerView.isNestedScrollingEnabled = false,但运气不佳。我也尝试将 RecyclerView 放入 NestedScrollView,但不推荐这样做,因为 RecyclerView 然后创建它需要的数据集的每个 ViewHolder,这显然效率不高。
vertical-scrolling android-fragments android-recyclerview android-nestedscrollview android-viewpager2
我将 Paging Library 3 与 RemoteMediator 一起使用,其中包括从网络和本地 Room 数据库加载数据。每次我滚动到 RecyclerView 中的某个位置,导航到另一个片段,然后导航回带有列表的片段时,滚动状态不会保留,并且 RecyclerView 显示从第一个项目开始的列表而不是位置在我离开之前我就已经到了。
我尝试使用 StateRestorationPolicy 但没有成功,并且似乎无法找到一种方法来获取 PagingDataAdapter 的滚动位置并在导航回 Fragment 时将其恢复到相同的精确位置。
在我的 ViewModel 中,我有一个从 RemoteMediator 收集数据的 Flow:
val flow = Pager(config = PagingConfig(5), remoteMediator = remoteMediator) {
dao?.getListAsPagingSource()!!
}.flow.cachedIn(viewModelScope)
Run Code Online (Sandbox Code Playgroud)
我正在将该数据提交给我的片段中的适配器:
viewLifecycleOwner.lifecycleScope.launch {
viewModel.flow.collectLatest { pagingData ->
adapter?.submitData(pagingData)
}
}
Run Code Online (Sandbox Code Playgroud)
在片段的顶部,我的适配器列出为:
class MyFragment : Fragment() {
...
private var adapter: FeedAdapter? = null
...
override onViewCreated(...) {
if (adapter == null) {
adapter = FeedAdapter(...)
}
recyclerView.adapter = adapter
viewLifecycleOwner.lifecycleScope.launch {
viewModel.flow.collectLatest …Run Code Online (Sandbox Code Playgroud) paging android android-paging android-paging-library android-paging-3
我在 RecyclerView 中有一个简单的 Pokemon 列表,只有 Pokemon 的名字和一个“收藏夹”切换按钮。我正在使用 Android JetPack 的 Paging Library 和 PageKeyedDataSource 来检索小块 Pokemon 并将它们显示给用户。我只希望只要 Activity 没有被销毁,数据就可以持久化(即我不想将数据持久化到 Room 或数据库,而是只要 ViewModel 还活着就让它持久化)。
我希望能够单击任何 Pokemon 项目上的心形按钮并将 SimplePokemon 模型中的“isFavorite”字段更新为true或false。根据我的理解,如果我想更改该 PagedList 中的单个项目,我需要使 DataSource 无效,并且理论上应该生成一个新的 PagedList LiveData,该 LiveData 可以提供给适配器并显示在屏幕上。
问题:如何在不需要 Room 或其他数据库的情况下使用分页库更新 PagedList 中的单个项目?
将来,我想将此解决方案扩展到用户可以喜欢帖子的社交媒体提要,但我不知道将社交提要项存储在 Room 等数据库中是否必要(或有效),因为这些提要项不断改变。所以我选择将它们存储在 ViewModel 中,然后在用户每次退出应用程序时清除它们。
到目前为止,这是我的代码:
SimplePokemon.kt:
data class SimplePokemon(
@SerializedName("name") val name: String,
@SerializedName("url") val url: String,
var isFavorite: Boolean = false
)
Run Code Online (Sandbox Code Playgroud)
PokemonViewModel.kt:
class PokemonViewModel(application: Application) : AndroidViewModel(application) {
private val config …Run Code Online (Sandbox Code Playgroud) paging android pagedlist android-paging android-paging-library
我有一个显示图像的 CollapsingToolbarLayout(放置在 ConstraintLayout 内以保持比例为 1:1)和一个com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior附加到它的 RecyclerView 。
图像为绿色,RecyclerView 为蓝色。
问题是当我在 RecyclerView 上向上滚动时,CollapsingToolbarLayout 不会折叠。相反, RecyclerView 只是在整个布局下方滚动。我必须在 CollapsingToolbarLayout 内手动滚动才能折叠。
这是布局的代码:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:nestedScrollingEnabled="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/app_bar" …Run Code Online (Sandbox Code Playgroud) scroll android-recyclerview android-coordinatorlayout android-collapsingtoolbarlayout android-nestedscrollview