Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

拖动展示列表跟实际数据列表不一致 #329

Open
yeagle2468 opened this issue Jan 6, 2024 · 1 comment
Open

拖动展示列表跟实际数据列表不一致 #329

yeagle2468 opened this issue Jan 6, 2024 · 1 comment

Comments

@yeagle2468
Copy link

不知道为何,这个bug一直没人提,原因出在这里:
Collections.swap(data, fromPosition, toPosition); // 这里是交换 fromPosition 的数据跟 toPosition数据进行交换
adapter.notifyItemMoved(fromPosition, toPosition); // 这里是将fromPosition 的移动到 toPosition

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
当fromPosition跟toPosition是临近的则没有问题,如9移动到8,正常;但当9直接移动到7就错了,展示的数据是[0, 1, 2, 3, 4, 5, 6, 9, 7, 8],而如果直接使用Collections.swap,则实际数据是 [0, 1, 2, 3, 4, 5, 6, 9, 8, 7],就会导致不一致,所有这里使用Collections.swap有问题

@luqiming666
Copy link

目前的演示代码中,都是相邻两个元素进行交换,所以不会出现你说的问题:

=== DragGridActivity ===
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++)
Collections.swap(mDataList, i, i + 1);
} else {
for (int i = fromPosition; i > toPosition; i--)
Collections.swap(mDataList, i, i - 1);
}

=== DragTouchListActivity ===
拖动时,相邻两个元素交换都会调用onItemMove,会被多次调用。
onItemMove() {
Collections.swap(mDataList, fromPosition, toPosition);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants