RecyclerView.Adapter.notifyItemMoved(0,1)滚动屏幕

Ari*_*Ari 7 android android-adapter android-recyclerview

我有一个由LinearlayoutManager管理的RecyclerView,如果我将项目1与0交换然后调用mAdapter.notifyItemMoved(0,1),则移动动画会导致屏幕滚动.我该怎样预防呢?

And*_*ger 7

可悲的是,yigit提出的解决方法滚动RecyclerView到顶部.这是我发现的最好的解决方法:

// figure out the position of the first visible item
int firstPos = manager.findFirstCompletelyVisibleItemPosition();
int offsetTop = 0;
if(firstPos >= 0) {
    View firstView = manager.findViewByPosition(firstPos);
    offsetTop = manager.getDecoratedTop(firstView) - manager.getTopDecorationHeight(firstView);
}

// apply changes
adapter.notify...

// reapply the saved position
if(firstPos >= 0) {
    manager.scrollToPositionWithOffset(firstPos, offsetTop);
}
Run Code Online (Sandbox Code Playgroud)

  • 令人难以置信,3.5 年后,这个错误仍然存​​在。然而,这个解决方案非常完美。 (5认同)

yig*_*git 5

scrollToPosition(0)移动物品后打电话.不幸的是,我假设,LinearLayoutManager尝试保持第一项稳定,这会移动,因此它会随之移动列表.