如何在notifyDataSetChanged之后阻止RecyclerView中的自动滚动?

BAr*_*ell 2 android android-recyclerview

我有RecyclerView几个项目类型.它看起来像这样:

  • 2017年7月21日
  • 消息1
  • 消息2
  • 消息3
  • 2017年7月22日
  • 消息4
  • 新邮件标题
  • 消息5
  • 消息6

当新消息到达时,我按日期和电话排序notifyDataSetChanged().它运作良好,但它会自动滚动我RecyclerView的新项目高度.在这种情况下,我无法使用,notifyItemInserted()因为我不知道排序后的项目位置.

没有通话RecyclerView后如何强制不滚动?notifyDataSetChanged()notifyItemInserted()

nhp*_*nhp 7

有4种方法可以尝试.因为我不知道你的视图的高度是多少,所以我不确定它是否真的有用.但是试一试吧

I.您没有可以更改哪个特定项目.但是你知道这种变化的范围

mAdapter.notifyItemInserted(position);                 
mAdapter.notifyItemRangeChanged(0, list.size());
Run Code Online (Sandbox Code Playgroud)

II.通话前清除清单notifyDataSetChanged()

yourList.clear();
mAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)

III.保存/重置ReyclerView添加新项目之前/之后的状态

    // Save state
    private Parcelable recyclerViewState;
    recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();

    // Restore state
    recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
Run Code Online (Sandbox Code Playgroud)

IV.使用此功能

mAdapter.notifyItemRangeChanged(0, list.size());
Run Code Online (Sandbox Code Playgroud)


Llu*_*art 1

你玩过吗myRecView.getLayoutManager().setStackFromEnd(boolean)

如果您可以确定在可见部分上方或下方排序后是否已插入新项目,并且在此调用函数中setStackFromEnd ()使用 true 或 false,也许您可​​以避免滚动。

不知道是否真的能起到作用......