我有一个ActionMode的实现来显示多个的数量 RecyclerView中的所选项目。
我想知道何时点击actionMode中的后退按钮以便相应地重置recyclelerView但是在实现ActionMode.Callback时,我注意到每当ActionMode更新actionMode?.setTitle($selectedItems.size)
时调用onDestroyActionMode,无法重置recyclerView - 删除所选项目,删除叠加颜色并通知Recyclerview已更改的数据集。
这是我的回叫
inner class ActionModeCallback : ActionMode.Callback {
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
when (item?.getItemId()) {
R.id.action_delete -> {
myAdapter?.deleteSelectedIds()
actionMode?.setTitle("") //remove item count from action mode.
actionMode?.finish()
return true
}
}
return false
}
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
val inflater = mode?.getMenuInflater()
inflater?.inflate(R.menu.action_mode_menu, menu)
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
menu?.findItem(R.id.action_delete)?.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
return true
}
override fun onDestroyActionMode(mode: ActionMode?) {
Log.d(TAG, "onDestroyActionMode Called")
//myAdapter?.selectedIds?.clear()
//myAdapter?.notifyDataSetChanged()
actionMode = null
}
}
我怎么知道何时点击了ActionMode后退按钮? 完整源代码Here => https://github.com/Edge-Developer/RecyclerViewMultiSelectExample
答案 0 :(得分:2)
这是我的代码
actionMode = startActionMode(ActionModeCallback())
这是更新后的代码
if (actionMode == null) actionMode = startActionMode(ActionModeCallback())
现在,在关闭actionMode后,onDestroyActionMode只被调用一次!
您可以查看the github repo如何实施