RecyclerView notifyDataSetChanged滚动到顶部位置

Moh*_*bdu 17 android android-appcompat android-recyclerview

打电话时notifyDataSetChangedRecyclerView 其滚动位置和卷轴不保留顶部,有没有解决方案,保留它的滚动位置?

ser*_*yuz 14

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

有两种解决方案:

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

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

第二种解决方案更有效率,我建议这一点


use*_*265 8

不确定这是否解决了您的问题,但我遇到了同样的问题,这是因为我在通知后调用了setAdapter.因此,在通知解决问题后不调用setAdapter.

mAdapter.notifyItemRangeInserted(mAdapter.getItemCount(), list.size() - 1);
recyclerView.setAdapter(); // remove this line. 
//Do this only when you're setting the data for the first time or when adapter is null. 
Run Code Online (Sandbox Code Playgroud)


小智 1

您可以使用swapAdapter()并在调用notificationDataSetChange()之后。这对我有用。