如果使用Paging 3库,则onRestoreInstanceState不适用于RecyclerView布局管理器

编程入门 行业动态 更新时间:2024-10-25 16:16:49
本文介绍了如果使用Paging 3库,则onRestoreInstanceState不适用于RecyclerView布局管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在保存RecyclerView状态时遇到问题,它通过保存布局管理器状态并在恢复片段后使用它来解决.(感谢@HarisDautović)

I had a problem in saving RecyclerView state and it solved by saving layout manager state and using it after resume fragment.(thanks to @HarisDautović)

class TestFragment : Fragment() { private val testListAdapter: TestListAdapter by lazy { TestListAdapter() } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_test, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) postListView.apply { layoutManager = StaggeredGridLayoutManager( 2, StaggeredGridLayoutManager.VERTICAL ).apply { gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS } setHasFixedSize(true) adapter = testListAdapter } } private var layoutManagerState: Parcelable? = null override fun onPause() { saveLayoutManagerState() super.onPause() } override fun onViewStateRestored(savedInstanceState: Bundle?) { super.onViewStateRestored(savedInstanceState) restoreLayoutManagerState() } private fun restoreLayoutManagerState () { layoutManagerState?.let { postListView.layoutManager?.onRestoreInstanceState(it) } } private fun saveLayoutManagerState () { layoutManagerState = postListView.layoutManager?.onSaveInstanceState() } }

,但是如果使用分页3库,则它不起作用.只是导入此库会导致问题,即使不在应用程序中使用它也是如此.

but if using paging 3 library it does not work. just importing this library causes problem even not using it in app.

请查看此问题和接受的答案的评论以获取更多详细信息:在ViewPager中带有StaggeredGridLayoutManager的RecyclerView,当返回片段时会自动排列项目

please see this question and accepted answer's comments for more details: RecyclerView with StaggeredGridLayoutManager in ViewPager, arranges items automatically when going back to fragment

推荐答案

更新:

我的问题通过使用解决:

my problem solved by using:

testListAdapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT

和自定义恢复逻辑

旧答案:问题出在Recyclervew.使用较新的alpha版本的recyclerView的分页库存在此问题.通过导入分页,整个项目都使用此版本的recyclerview.强制使用稳定版的RecyclerView解决了该问题.

OLD ANSWER: problem is from Recyclervew. the paging library using a newer alpha version of recyclerView that have this problem. by importing paging, whole project using this version of recyclerview. forcing to use stable version of RecyclerView solve the problem.

在build.gradle中:

in build.gradle:

android { ... configurations.all { resolutionStrategy.eachDependency { details -> if (details.requested.group == 'androidx.recyclerview') { details.useVersion "1.1.0" } } } }

更多推荐

如果使用Paging 3库,则onRestoreInstanceState不适用于RecyclerView布局管理器

本文发布于:2023-11-27 05:08:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   布局   不适用于   Paging   onRestoreInstanceState

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!