gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:22 28 4
gpt4 key购买 nike

我有一个由 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com