ListView:隐藏上下文操作栏

时间:2014-06-26 08:08:51

标签: android listview android-contextmenu multichoiceitems

我跟这样的official guide for using contextual action mode

    listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.shelf_context, menu);
            return true;
        }

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

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // some processing...
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
        }
    });

我的listView位于Fragment内的ViewPager内,因此我希望在片段变得不可见时隐藏上下文操作栏。但是我该怎么做?我调用listView.clearChoices()来清除选择,但CAB仍然可见:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (!isVisibleToUser) {
        listView.clearChoices(); // CAB is still visible.
    }
}

2 个答案:

答案 0 :(得分:4)

mode.finish()没有工作?

case R.id.action_remove:
    mode.finish();

答案 1 :(得分:0)

经过一番研究后,我发现了一种愚蠢的方法:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (!isVisibleToUser) {
        listView.clearChoices();
        listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL); // this strange hack dismisses the CAB.
    }
}