Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 工具栏不会在动作模式下消失大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试实现这样的功能:当我长按一个列表项时,动作模式应该开始,并且可以删除一个或多个项目.
我在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

当我长时间点击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

@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找到

大佬总结

以上是大佬教程为你收集整理的android – 工具栏不会在动作模式下消失全部内容,希望文章能够帮你解决android – 工具栏不会在动作模式下消失所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。