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

查看:723
本文介绍了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天全站免登陆