RecyclerView notifyDataSetChanged 滚动到顶部位置 [英] RecyclerView notifyDataSetChanged scrolls to top position

查看:57
本文介绍了RecyclerView notifyDataSetChanged 滚动到顶部位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在 RecyclerView 上调用 notifyDataSetChanged 时,它不保留滚动位置并滚动到顶部,有没有办法保留它的滚动位置?

when calling notifyDataSetChanged on RecyclerView it doesn't retain the scroll position and scrolls to top, is there any solution to retain it's scroll position?

推荐答案

如果您的 RecyclerView 列表项父项具有wrap_content"属性,则 Recyclerview 会再次计算高度并滚动顶部.

If your RecyclerView list item parent has "wrap_content" property, Recyclerview calculates the heights again and scrolls top.

有两种解决方案:

  1. 将您的高度设置为一个常量值,如下所示:layout_height="100dp"
  2. 像这样使用 StaggeredGridLayoutManager:

  1. Set your height a constant value like this: layout_height="100dp"
  2. Use StaggeredGridLayoutManager like this:

mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));

第二种解决方案更有效,我推荐这个

The second solution is more efficient and I suggest this one

这篇关于RecyclerView notifyDataSetChanged 滚动到顶部位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆