Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus control in a ListView

The context: I want to have a ListView that wouldn't receive focus (e.g. won't highlight row when user touches it). Yet each row widget has it's own OnClickListener. Here's what I specify in layout xml:

android:choiceMode="none" 
android:focusableInTouchMode="false"
android:focusable="false"

The ListView still behaves exactly the same. Could someone please explain

  1. The interrelation between the three
  2. What's the right way to create a ListView that doesn't receive focus?

TIA.

like image 239
yanchenko Avatar asked Dec 13 '25 19:12

yanchenko


2 Answers

Although you specified in the xml you could try specifying in code as well. Although i'm not sure you can set a list to not focusable and still have the list be scrollable and it's clickable.

after your setContentView...

myListView.setFocusableInTouchMode(false);

You could try and inherit from the view as well and then add a little debugging code in the interim to help you find when the list actually has focus.

    myListView.setOnFocusChangeListener(new OnFocusChangeListener(){

        public void onFocusChange(View v, boolean hasFocus)
        {
            v.setBackgroundColor(hasFocus ? Color.GRAY : Color.BLACK);
        }
    });

    myListView.setClickable(true);
like image 77
Quintin Robinson Avatar answered Dec 16 '25 10:12

Quintin Robinson


The list receiving focus is different than the selected row not highlighting. The list gets focus whenever a user is in it. The best a ListView can do is report an int for whatever the user has selected. I'm not sure how each row widget has it's own ClickListener. There are no row widgets that I am aware of. The onListItemClick belongs to the ListView.

I havent been able to figure it out yet, but between android:listSelector and android:background and adjusting the alpha channels I figure there would be a way to make a selection look just like a non-selected row.

android:listSelector="#8fff" makes it so just the foreground changes on selection.

It seems like android chooses the non-selected foreground on its own, which is making this hard. I hope this helps.

like image 22
Will Avatar answered Dec 16 '25 10:12

Will



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!