未显示 ActionMode 菜单图标
ActionMode menu icon is not shown
我正在尝试在回收站视图中长按某个项目时设置上下文操作栏。但是没有显示菜单图标。
代码如下。
listAdapter.setOnItemClickListener(object : OnItemClickListener {
override fun onItemClick(position: Int) {
...
}
override fun onItemLongClick(position: Int): Boolean {
if (actionMode != null) {
return false
}
val activity = activity as AppCompatActivity
actionMode = activity.startSupportActionMode(actionModeCallback)
return true
}
})
动作模式回调
internal val actionModeCallback = object : ActionMode.Callback {
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.item_delete -> {
actionMode?.finish()
return true
}
}
return false
}
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
actionMode?.menuInflater?.inflate(R.menu.menu_context, menu)
actionMode?.title = "delete"
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
actionMode = null
}
}
menu_context.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item_delete"
android:title="@string/menu_option_text_delete"
android:icon="@drawable/ic_delete_white_24dp"
app:showAsAction="always"
/>
</menu>
我尝试了什么?
- 对
Activity
和 ActionMode
使用不支持的库版本
- 尝试更改动作模式背景以查看主题是否存在问题
- set SHOW_AS_ACTION_ALWAYS in onPrepareActionMode
没用,连动作模式的标题都没有显示。
我正在使用 androidx,appCompat 版本:1.1.0-rc01
请帮忙!
解决方法是在onPrepare ActionMode中手动更新菜单
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
menu.findItem(R.id.menu_archive).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.menu_delete).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.menu_upload_to_cloud).setShowAsAction
(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
实际上,正如 issue tracker
中指出的那样,这是我的错误
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
/*-->*/mode?.menuInflater?.inflate(R.menu.menu_context, menu)
return true
}
Note that you should use the passed in mode and not your own
ActionMode object to get the correct inflater.
我正在尝试在回收站视图中长按某个项目时设置上下文操作栏。但是没有显示菜单图标。
代码如下。
listAdapter.setOnItemClickListener(object : OnItemClickListener {
override fun onItemClick(position: Int) {
...
}
override fun onItemLongClick(position: Int): Boolean {
if (actionMode != null) {
return false
}
val activity = activity as AppCompatActivity
actionMode = activity.startSupportActionMode(actionModeCallback)
return true
}
})
动作模式回调
internal val actionModeCallback = object : ActionMode.Callback {
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.item_delete -> {
actionMode?.finish()
return true
}
}
return false
}
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
actionMode?.menuInflater?.inflate(R.menu.menu_context, menu)
actionMode?.title = "delete"
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
actionMode = null
}
}
menu_context.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item_delete"
android:title="@string/menu_option_text_delete"
android:icon="@drawable/ic_delete_white_24dp"
app:showAsAction="always"
/>
</menu>
我尝试了什么?
- 对
Activity
和ActionMode
使用不支持的库版本
- 尝试更改动作模式背景以查看主题是否存在问题
- set SHOW_AS_ACTION_ALWAYS in onPrepareActionMode
没用,连动作模式的标题都没有显示。
我正在使用 androidx,appCompat 版本:1.1.0-rc01
请帮忙!
解决方法是在onPrepare ActionMode中手动更新菜单
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
menu.findItem(R.id.menu_archive).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.menu_delete).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.menu_upload_to_cloud).setShowAsAction
(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
实际上,正如 issue tracker
中指出的那样,这是我的错误override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean { /*-->*/mode?.menuInflater?.inflate(R.menu.menu_context, menu) return true }
Note that you should use the passed in mode and not your own ActionMode object to get the correct inflater.