Search code examples
androidlistviewmultichoiceitems

How do I prevent clicking elements in a ListView row when in MutliChoiceMode in Android


I have a Listview set up with a custom adapter. Each of the ListView's rows has a button and a seekbar that the user can interact with. I have also implemented a MultiChoiceModeListener to handle long clicking a ListView row in order to allow the user to delete multiple rows.

What I need is to prevent the user from interacting with each of the ListView's row elements (buttons, seekbars etc) when the ListView is in MultiSelect mode, by disabling them, and then to enable them once the user exits the multiselect mode.

Is there a simple way to do this? I've tried adding:

    for(int i = 0; i < adapter.getCount(); i++){
        View v = promptListView.getChildAt(i);
        v.setEnabled(false);
    }

to the onCreateActionMode() method, but with no success, I'm still able to interact with the row elements. Any help would be greatly appreciated, thanks.


Solution

  • Add the property android:descendantFocusability="blocksDescendants" to the layout root of your list row. This will prevent the children from gaining focus, and will enable the user to click on the list row or any of its child views independently. Now you can disable (gray out) and re-enable any of the child views by using setEnabled() like you do in your code. Or if you would just like to prevent the user from being able to interact with a view without graying it out, you can use setClickable() instead.