attachToRecyclerView 不会从 RecyclerView 中删除以前的 ItemHelper
attachToRecyclerView doesn't remove previous ItemHelper from RecyclerView
我有一个 RecyclerView,它具有用于删除行的滑动手势。 RecyclerView 的适配器数据会不时重置,在这种情况下,我会将其适配器重新分配给一个新适配器。虽然新数据似乎反映在 UI 中(即 RecyclerView 更新),但滑动手势仍然反映旧适配器。我原以为 attachToRecyclerView()
会处理旧的 ToucherHelper 的删除。根据 attachToRecyclerView()
的文档:
Attaches the ItemTouchHelper to the provided RecyclerView. If TouchHelper is already attached to a RecyclerView, it will first detach from the previous one. You can call this method with null to detach it from the current RecyclerView.
但是,我的日志显示调用 onSwiped()
时适配器的地址仍然是旧适配器的地址。
日志
HistorySwipeHelper init itemCount 0 address 156969509
HistorySwipeHelper init itemCount 3 address 65479865
onSwiped itemCount 0 adapterPosition 0 address 156969509
MyFragment 中的 setupRecyclerView()
private fun setupRecyclerView(historyList: MutableList<HistoryObject>) {
val historyListAdapter = HistoryListAdapter(historyList)
val callback = HistorySwipeHelper(historyListAdapter)
val helper = ItemTouchHelper(callback)
history_games_list.adapter = historyListAdapter
helper.attachToRecyclerView(history_games_list)
}
HistorySwipeHelper
class HistorySwipeHelper(private val adapter: HistoryListAdapter) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
init {
Log.d("HistorySwipeHelper", "HistorySwipeHelper init itemCount ${adapter.itemCount} address ${System.identityHashCode(adapter)}")
}
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { return false }
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
Log.d("HistorySwipeHelper", "onSwiped itemCount ${adapter.itemCount} adapterPosition ${viewHolder.adapterPosition} address ${System.identityHashCode(adapter)}")
if (viewHolder.adapterPosition < adapter.itemCount) {
adapter.showMenu(viewHolder.adapterPosition)
}
}
}
我可以在存储对当前 ItemHelper 的引用的 Fragment 上创建一个成员变量,并在附加新的之前将其分离。这行得通,但感觉就像代码的味道。有没有一种惯用的方法可以让我的滑动手势与它们所连接的 RecyclerView 保持同步?
尝试手动附加 RecyclerView
class HistorySwipeHelper(private val adapter: HistoryListAdapter) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
init {
Log.d("HistorySwipeHelper", "HistorySwipeHelper init itemCount ${adapter.itemCount} address ${System.identityHashCode(adapter)}")
}
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { return false }
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
Log.d("HistorySwipeHelper", "onSwiped itemCount ${adapter.itemCount} adapterPosition ${viewHolder.adapterPosition} address ${System.identityHashCode(adapter)}")
if (viewHolder.adapterPosition < adapter.itemCount) {
adapter.showMenu(viewHolder.adapterPosition)
}
}
}.attachToRecyclerView(yourRecyclerView);
我有一个 RecyclerView,它具有用于删除行的滑动手势。 RecyclerView 的适配器数据会不时重置,在这种情况下,我会将其适配器重新分配给一个新适配器。虽然新数据似乎反映在 UI 中(即 RecyclerView 更新),但滑动手势仍然反映旧适配器。我原以为 attachToRecyclerView()
会处理旧的 ToucherHelper 的删除。根据 attachToRecyclerView()
的文档:
Attaches the ItemTouchHelper to the provided RecyclerView. If TouchHelper is already attached to a RecyclerView, it will first detach from the previous one. You can call this method with null to detach it from the current RecyclerView.
但是,我的日志显示调用 onSwiped()
时适配器的地址仍然是旧适配器的地址。
日志
HistorySwipeHelper init itemCount 0 address 156969509
HistorySwipeHelper init itemCount 3 address 65479865
onSwiped itemCount 0 adapterPosition 0 address 156969509
MyFragment 中的 setupRecyclerView()
private fun setupRecyclerView(historyList: MutableList<HistoryObject>) {
val historyListAdapter = HistoryListAdapter(historyList)
val callback = HistorySwipeHelper(historyListAdapter)
val helper = ItemTouchHelper(callback)
history_games_list.adapter = historyListAdapter
helper.attachToRecyclerView(history_games_list)
}
HistorySwipeHelper
class HistorySwipeHelper(private val adapter: HistoryListAdapter) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
init {
Log.d("HistorySwipeHelper", "HistorySwipeHelper init itemCount ${adapter.itemCount} address ${System.identityHashCode(adapter)}")
}
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { return false }
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
Log.d("HistorySwipeHelper", "onSwiped itemCount ${adapter.itemCount} adapterPosition ${viewHolder.adapterPosition} address ${System.identityHashCode(adapter)}")
if (viewHolder.adapterPosition < adapter.itemCount) {
adapter.showMenu(viewHolder.adapterPosition)
}
}
}
我可以在存储对当前 ItemHelper 的引用的 Fragment 上创建一个成员变量,并在附加新的之前将其分离。这行得通,但感觉就像代码的味道。有没有一种惯用的方法可以让我的滑动手势与它们所连接的 RecyclerView 保持同步?
尝试手动附加 RecyclerView
class HistorySwipeHelper(private val adapter: HistoryListAdapter) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
init {
Log.d("HistorySwipeHelper", "HistorySwipeHelper init itemCount ${adapter.itemCount} address ${System.identityHashCode(adapter)}")
}
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { return false }
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
Log.d("HistorySwipeHelper", "onSwiped itemCount ${adapter.itemCount} adapterPosition ${viewHolder.adapterPosition} address ${System.identityHashCode(adapter)}")
if (viewHolder.adapterPosition < adapter.itemCount) {
adapter.showMenu(viewHolder.adapterPosition)
}
}
}.attachToRecyclerView(yourRecyclerView);