我正在寻找有关如何在ListView项目的工具栏中实现上下文菜单的帮助,就像WhatsApp那样。到目前为止,我发现的唯一教程是关于弹出的对话框,这不是我想要的。有人可以帮助我或提供教程链接吗?谢谢:))
答案 0 :(得分:6)
检查this official android guide.
修改强>
使用上下文操作模式
对于提供上下文操作的视图,通常应该在两个事件之一(或两者)上调用上下文操作模式:
您的应用程序如何调用上下文操作模式并定义每个操作的行为取决于您的设计。基本上有两种设计:
为各个视图启用上下文操作模式
实现ActionMode.Callback接口。在其回调方法中,您可以指定上下文操作栏的操作,响应操作项上的单击事件,以及处理操作模式的其他生命周期事件。
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
// Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_share:
shareCurrentItem();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
如果要显示栏,请调用startActionMode()(例如,当用户长按视图时)。
someView.setOnLongClickListener(new View.OnLongClickListener() {
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
// Start the CAB using the ActionMode.Callback defined above
mActionMode = getActivity().startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
在ListView或GridView中启用批量上下文操作
如果ListView或GridView(或AbsListView的另一个扩展)中有一组项目,并且想要允许用户执行批处理操作,您应该:
使用CHOICE_MODE_MULTIPLE_MODAL参数调用setChoiceMode()。
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
有关更多菜单功能check this link.