2

我正在按照官方指南使用这样的上下文操作模式:

    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 在 aFragment里面ViewPager,所以我想在片段变得不可见时隐藏上下文操作栏。但是我该怎么做呢?我打电话listView.clearChoices()清除选择,但 CAB 仍然可见:

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

2 回答 2

5

mode.finish()不工作?

case R.id.action_remove:
    mode.finish();
于 2014-06-26T08:50:36.647 回答
0

经过一番研究,我发现了一种hacky方法:

@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.
    }
}
于 2014-06-26T08:10:50.533 回答