恢复recyclerview滚动位置

You might have had the problem where a RecyclerView loses the scroll position when your Activity/Fragment is re-created. This usually happens because the Adapter data is loaded asynchronously and data hasn’t loaded by the time RecyclerView needs to layout so it fails to restore the scroll position.

您可能遇到以下问题:重新创建“ Activity / Fragment时, RecyclerView失去滚动位置。 这通常是因为Adapter数据是异步加载的,而RecyclerView需要进行布局RecyclerView没有加载数据,因此它无法恢复滚动位置。

Starting with 1.2.0-alpha02, RecyclerView offers a new API to let the Adapter block layout restoration until it is ready. Read on to learn how to use this new API and how it works.

1.2.0-alpha02RecyclerView提供了一个新的API,可让Adapter 阻止布局恢复直到准备就绪 。 继续阅读以了解如何使用此新API及其工作方式。

恢复滚动位置 (Restoring the scroll position)

There are several ways to ensure a correct scroll position that you might have adopted. The best one is making sure that you always set the data on the Adapter before the first layout pass by caching the data you want to display in memory, in a ViewModel or in a repository. If this approach wasn’t possible, other solutions were either more complicated, like avoiding setting the Adapter on the RecyclerView, which can bring issues with items like headers, or misusing LayoutManager.onRestoreInstanceState API.

有几种方法可以确保您可能已采用正确的滚动位置。 最好的方法是通过在缓存中, ViewModel或存储库中缓存要显示的数据,确保始终在第一个布局通过之前在Adapter上设置数据。 如果无法采用这种方法,则其他解决方案可能会更加复杂,例如避免在RecyclerView上设置Adapter ,这可能会导致标题等项目出现问题,或者滥用LayoutManager.onRestoreInstanceState API。

The recyclerview:1.2.0-alpha02 solution is a new Adapter method which allows you to set a state restoration policy (via the StateRestorationPolicy enum). This has 3 options:

recyclerview:1.2.0-alpha02解决方案是一种新的Adapter方法,它允许您设置状态恢复策略(通过StateRestorationPolicy枚举)。 这有3个选项:

  • ALLOW — the default state, that restores the RecyclerView state immediately, in the next layout pass

    ALLOW默认状态,在下一次布局传递中立即恢复RecyclerView状态

  • PREVENT_WHEN_EMPTY — restores the RecyclerView state only when the adapter is not empty (adapter.getItemCount() > 0). If your data is loaded async, the RecyclerView waits until data is loaded and only then the state is restored. If you have default items, like headers or load progress indicators as part of your Adapter, then you should use the PREVENT option, unless the default items are added using ConcatAdapter (find out more here). ConcatAdapter waits for all of its adapters to be ready and only then it restores the state.

    PREVENT_WHEN_EMPTY —仅在适配器不为空( adapter.getItemCount() > 0 )时才恢复RecyclerView状态。 如果您的数据是异步加载的,则RecyclerView会一直等到数据加载后才恢复状态。 如果您有默认项,例如Adapter标题或加载进度指示符,那么您应该使用PREVENT选项,除非使用ConcatAdapter添加了默认项( 在此处查找更多信息 )。 ConcatAdapter等待其所有适配器准备就绪,然后才恢复状态。

  • PREVENT — all state restoration is deferred until you set ALLOW or PREVENT_WHEN_EMPTY.

    PREVENT所有状态恢复推迟到您设置ALLOWPREVENT_WHEN_EMPTY

Set the state restoration policy on the adapter like this:

像这样在适配器上设置状态恢复策略:

adapter.stateRestorationPolicy = PREVENT_WHEN_EMPTY

That’s it! A short and sweet post to get you up to date with RecyclerView’s lazy state restoration feature. Start using it 🏁👩‍💻👨‍💻!

而已! 简短而有趣的文章,让您了解RecyclerView的惰性状态恢复功能。 开始使用🏁👩‍💻👨‍💻!

翻译自: https://medium.com/androiddevelopers/restore-recyclerview-scroll-position-a8fbdc9a9334

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值