Android: disable ListView selection
Asked Answered
D

4

22

I have a listView that I re-display with the correct answer highlighted when the user selects an item. However, at that point I would like to disable selection of list view items; the user can only get to the next question by pressing a button. However, even if I disable the items like so:

   private void disableItemSelection() {
        ListView lv = getListView();
        for (int i = 0; i < lv.getChildCount(); i++){
            View v = lv.getChildAt(i);
            v.setEnabled(false);
        }
    }

...it still highlights the selection when the user selects it. Any thoughts?

Damicke answered 4/6, 2011 at 13:56 Comment(2)
Did u find a solution to this?Assemblyman
No - I did a workaround which takes them to the next question if they try to select a button again.Damicke
H
32

The reason the scrolling is locked is because you are setting up at

ListView

I found a way to disable highlighting without locking the scroll. You need to set it up at ListAdapter as shown below:

ArrayAdapter<String> myList = new ArrayAdapter<String>(this, R.layout.list_item, strText) {
           public boolean isEnabled(int position) 
            { 
                    return false; 
            } 
        };
Hurds answered 9/2, 2012 at 9:20 Comment(2)
You don't need to use an ArrayAdapter, if you don't want to. Just override the isEnabled method on your own custom adapter and you're good to go!Crural
doesn't it mean that if there is a drawable for the rows that has the disabled state , it will also change their image ? is it possible to disable clicking and yet not change the drawable ?Rollandrollaway
A
29

You can add attribute to Listview XML layout to disable multiple high light rows as

android:listSelector="@android:color/transparent"               
android:cacheColorHint="@android:color/transparent"
Ailanthus answered 15/5, 2013 at 3:48 Comment(2)
I think this is the best ansewr, is always best to set this details in the xml than in the code.Counteraccusation
For me it was enough just to set listSelector.Yseulte
F
4

I just hid the selected item with this:

listView.setSelector(new ColorDrawable(0));

And then you show it again by restoring to whatever drawable it was using previously:

wifiSsid.setSelector(R.drawable.listview_selector);
Faliscan answered 21/2, 2013 at 4:11 Comment(0)
C
2

I used android:choiceMode="none" as documented here.

Camass answered 1/5, 2013 at 17:30 Comment(1)
This only sets if list items can have a selected mode, but not the highlighting :(Salience

© 2022 - 2024 — McMap. All rights reserved.