Monday, 27 July 2015

reset listview mode from multi-select (multi-choice) to normal (from CHOICE_MODE_MULTIPLE_MODAL)

first: listView.clearChoices();  is rubbish, it doesn't work




1, if you are in the MultiChoiceModeListener, you can:

    mode.finish();
2. if you are out side of the MultiChoiceModeListener:
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

above code can finish the mode.

It can finish the multi selection mode and clean all your selected items. However, it is single choice, so when you click one item on the list, it will highlight it first (it doesn't impact if you want to show the detail page).

    listView.setChoiceMode(ListView.CHOICE_MODE_NONE);


Hence, the above code is the right mode. However if you do this change, it can finish the multi selection mode but it still keep showing your selected Items.

If you think do these commands together, they will solve the issue:

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

However, it gives you the same result as you only do the last line.

I think the system just doesn't apply the change immediately, so no matter what you did, it will apply the last one.

Solution:


private void resetListViewMode() {
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    listView.clearChoices();
    listView.post(new Runnable() {
        @Override        public void run() {
            listView.setChoiceMode(ListView.CHOICE_MODE_NONE);
        }
    });
}




Note:

You can use this function to programmatically select items

listView.setItemChecked(2, true);


http://stackoverflow.com/questions/13769762/enter-multi-choice-mode-with-nothing-selected
http://stackoverflow.com/questions/9754170/listview-selection-remains-persistent-after-exiting-choice-mode



















No comments:

Post a comment