18

我有一个 listActivity 在长按时显示 CAB。如果选择了超过 1 个项目,我想隐藏我的一个菜单项。

我跟踪在 中选择的项目数onItemCheckedStateChanged()。但是,我无权访问菜单以从此功能中删除该项目。请参阅下面代码中的注释以了解我在尝试什么。我觉得我缺少一些简单的核心理解......下面的代码是从我的onCreate()函数中调用的。

private void setupActionBarContext() {
    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        private int selCount = 0;
        ArrayList<Long> idList = new ArrayList<Long>();

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            if (checked) {
                selCount++;
                idList.add(id);
            } else {
                selCount--;
                idList.remove(id);
            }
            mode.setTitle(selCount + " selected");

            // I WOULD LIKE TO HIDE ITEM ON MENU IF 'selCount' IS > 1
            // For example something like this...

            //                if (selCount > 1) {
            //                    MenuItem item = menu.findItem(R.id.edit_item);
            //                    item.setVisible(false);
            //                } else {
            //                    MenuItem item = menu.findItem(R.id.edit_item);
            //                    item.setVisible(false);
            //                }

        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.delete_item:
                    for(Long i: idList){
                        mDbHelper.deleteItem(i);
                    }
                    mode.finish();
                    return true;
                case R.id.edit_item:
                    Toast.makeText(getBaseContext(), "Edit Item", Toast.LENGTH_SHORT).show();
                    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, menu);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            selCount = 0;
            idList.clear();
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }
    });

还有我的菜单项...

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/edit_item"
        android:title="@string/edit_item"
        android:showAsAction="ifRoom"
        android:orderInCategory="1"/>
    <item android:id="@+id/delete_item"
        android:title="@string/delete_item"
        android:icon="@drawable/ic_action_delete"
        android:showAsAction="ifRoom"
        android:orderInCategory="2"/>
</menu>
4

1 回答 1

31

正如 adneal 的评论中所建议的那样。

将 invalidate() 添加到onItemCheckedStateChanged()

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
     if (checked) {
         selCount++;
         idList.add(id);
     } else {
         selCount--;
         idList.remove(id);
     }
     mode.setTitle(selCount + " selected");

     mode.invalidate();  // Add this to Invalidate CAB
}

这会使 CAB 无效并导致onPrepareActionMode()调用该函数。

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
   if (selCount == 1){
       MenuItem item = menu.findItem(R.id.edit_item);
       item.setVisible(true);
       return true;
   } else {
       MenuItem item = menu.findItem(R.id.edit_item);
       item.setVisible(false);
       return true;
   }
}
于 2013-07-14T17:58:24.473 回答