我有一个由 LinearlayoutManager
管理的 RecyclerView
,如果我将项目 1 与 0 交换,然后调用 mAdapter.notifyItemMoved(0,1)
,移动动画导致屏幕滚动。我该如何预防?
最佳答案
遗憾的是,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);
}
关于android - RecyclerView.Adapter.notifyItemMoved(0,1) 滚动屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27992427/