gpt4 book ai didi

带有 itemTouchHelper 的 android 拖放导致 IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-29 01:29:25 25 4
gpt4 key购买 nike

我正在使用 itemTouchHelper 拖放/滑动删除我的 RecyclerView 列表 as described here 中的项目。这些项目可以毫无问题地切换/删除,但是由于列表项目上的按钮是在调用 onBindViewHolder 时创建的,因此按钮“记住”的 POSITION 不会更新,这会导致很多错误(主要是 IndexOutOfBound 异常)。请帮忙,我无法让它工作。

例如,假设我在列表中有 2 个项目,A 和 B。然后,我在它们之间切换以获得 B 和 A。然后,我单击 B 上的复选框(这会立即更新服务器)。当我查看服务器时,A 有一个选中的复选框,而不是 B(即使我点击了 B),因为位置没有在调用中更新:

mValues.get(position).put("checkbox",true)

代码如下:

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(mValues, i, i + 1);
notifyItemMoved(i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(mValues, i, i - 1);
notifyItemMoved(i, i-1);
}
}

return true;
}

和:

@Override
public void onItemDismiss(int position) {
if (mValues.size() > position) {
mValues.remove(position).deleteEventually();
notifyItemRemoved(position);
}
}

最佳答案

the POSITION that the button "remember" is not updated,

与 ListView 适配器中使用的 View “记住”位置的策略不同,RecyclerView 需要一种不同的方法。

参见 this SO answer示例解决方案。

关于带有 itemTouchHelper 的 android 拖放导致 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32106145/

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