我正在尝试实现这样的功能:当我长按一个列表项时,动作模式应该开始,并且可以删除一个或多个项目.
我在MainActivity DocumentsActivity中开始搜索,它使用ListView及其项启动Fragment DocumentsFragment. ListAdapter通过Fragment中onCreate中的方法调用setListAdapter(this.documentsAdapter)进行初始化和设置.我在片段中的onActivityCreated的listview上设置了各种监听器:
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> parent,View view,int position,long id) {
getListView().setItemChecked(position,true);
return true;
}});
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public boolean onCreateActionMode(ActionMode mode,Menu menu) {
menu.clear();
((DocumentsActivity)getActivity()).getMenuInflater().inflate(R.menu.documents_context_menu,menu);
return true;
}
});
super.onActivityCreated(savedInstanceState);
}
当我长时间点击listitem时,操作模式开始,菜单documents_context_menu似乎是操作栏.但问题是,操作栏出现在工具栏上方,工具栏不会消失(参见图片).
我试图调用getSupportActionBar().hide()或将其设置为null或甚至使用其他样式/主题.一切都行不通.有时蓝色工具栏是完全白色的,但这就是全部.
我完全不知道为什么工具栏不会消失.你能提一些建议吗?
提前致谢!
_____更新1 _____
这是styles.xml
这就是活动中设置操作栏的方式:
protected void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
requestWindowFeature(5);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_documents);
Toolbar mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
args = getIntent().getExtras();
if (findViewById(R.id.container_documents) != null && savedInstanceState == null) {
showDocumentsFragment();
}
}
最佳答案
所有这些答案都很好你应该尝试它们但你需要的是一个ContextualMenu所以你应该首先将视图添加到registerForContextMenu()所以菜单知道哪些菜单是上下文然后实现你的Activity的onCreateContextMenu
原文链接:https://www.f2er.com/android/430241.html@Override
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu,v,menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu,menu);
}
然后像这样实现onContextItemSelected():
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
然后你必须对视图执行一个动作并实现AbsListView.MultiChoiceModeListener然后使用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,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
所有我说的,最终更多,你可以在this android developer documentation找到