[英]Android: Recyclerview with ItemTouchHelper.Callback flickering on lower items in list
I am currently trying to implement a RecyclerView list with drag and drop reordering.我目前正在尝试通过拖放重新排序来实现 RecyclerView 列表。 For this I use the ItemTouchHelper.SimpleCallback为此,我使用 ItemTouchHelper.SimpleCallback
class SoftkeyScreenListReorderHelperCallback(
private val adapter: SoftkeyScreenListAdapter
) : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.START or ItemTouchHelper.END, 0) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return adapter.itemMoved(viewHolder.bindingAdapterPosition, target.bindingAdapterPosition)
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}
}
My Adapter got the itemMoved() method, which is called in the onMove() method in the callback.我的 Adapter 得到了 itemMoved() 方法,该方法在回调中的 onMove() 方法中调用。 Here I just swap the items and notify the adapter about the change.在这里,我只是交换项目并通知适配器有关更改。
fun itemMoved(fromPosition: Int, toPosition: Int): Boolean {
Collections.swap(list, fromPosition, toPosition)
notifyItemMoved(fromPosition, toPosition)
return true
}
For my RecyclerView I implemented the following对于我的 RecyclerView 我实现了以下
binding.recyclerview.apply {
[...] // adapter init
myAdapter.setHasStableIds(true)
adapter = myAdapter
val touchHelper = ItemTouchHelper(SoftkeyScreenListReorderHelperCallback(adapter as SoftkeyScreenListAdapter))
touchHelper?.attachToRecyclerView(this)
(itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
setHasFixedSize(true)
}
It works, but I always get flickering for the items below (after) the new item position.它有效,但我总是在新项目 position 之后(之后)闪烁。 Assume I have 5 Items {1,2,3,4,5} and want to swap 1 with 3, then 4 and 5 are flickering.假设我有 5 个项目 {1,2,3,4,5} 并且想将 1 与 3 交换,那么 4 和 5 正在闪烁。 1, 2 and 3 don't. 1、2和3没有。
I already set the recyclerview size fixed, enabled stable ids and disabled animations, but it does not help.我已经将 recyclerview 的大小设置为固定,启用了稳定的 id 并禁用了动画,但这并没有帮助。 Does anyone has a clue what could be the reason for that and how to fix?有谁知道这可能是什么原因以及如何解决?
Try this:尝试这个:
After adapter initialization:适配器初始化后:
adapter.setHasStableIds(true);
In adapter class:在适配器 class 中:
@Override
public long getItemId(int position) {
return itemList.get(position).hashCode();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.