enter multi-choice mode with nothing selected

自古美人都是妖i 提交于 2019-12-07 17:35:48

问题


our UX asks for a button to start multi-choice mode. this would do the same thing as long-pressing on an item, but would have nothing selected initially.

what i'm seeing in the code is that i cannot enter multi-choice mode mode unless i have something selected, and if i unselect that item, multi-choice mode exits (contextual action bar closes).

i've also tried this in other apps (gmail), and it works the same way.

is there a way to be in multi-select mode, with no items selected? thanks.


回答1:


It's very hacky, but I've done this by having an item selected, but making it look like it's not selected, by making the background temporarily transparent. When an item is then selected by the user, the secretly-selected item is deselected and the background restored to normal. Or, if it's the secretly-selected item which is selected (thus deselecting it), I reselect it, then set a boolean to stop it happening again.

I also had to use a counter in onItemCheckedStateChanged, as I was changing the checked state of the secret item from within that callback, resulting in a loop.

Probably not an ideal solution for all cases, but I don't think there's another way to do it at the moment, since AbsListView can't easily be extended.

Edit: if the screen orientation changes while the selected state of the selected item is hidden, it will suddenly be shown as being selected, so you have to make sure to save the fact that it should be hidden, and restore it after the listview is recreated. I had to use the View post() method to ensure the restoration happened after the listview had finished redrawing all its child items after the configuration change.

Edit: another potential issue is if the user tries to carry out an action while there are supposedly no items selected. As far as the application knows there is an item selected so it will carry out the action on that item, unless you make sure it doesn't.




回答2:


Just call:

mListView.setItemChecked(-1, true);

ListView's actionMode will be started without selecting any list element.

Make sure you've properly set your ListView before call:

mListView.setMultiChoiceModeListener( ...  )
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
or
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);



回答3:


You just have to use :

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);



回答4:


If you want to change the action bar, call this from your activity:

startActionMode(new ActionMode.Callback {

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

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

    @Override
    public void onDestroyActionMode(ActionMode mode) {

    }

  @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return false;
    }
});


来源:https://stackoverflow.com/questions/13769762/enter-multi-choice-mode-with-nothing-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!