3

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.

Zeeshan
  • 1,625
  • 17
  • 25
yanchenko
  • 56,576
  • 33
  • 147
  • 165

3 Answers3

6

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);
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • I don't understand why but setting `android:focusableInTouchMode="false"` in the view XML wouldn't stop it focusing/scrolling at runtime. Only calling `listView.setFocusableInTouchMode(false);` in code worked. Magic! – infl3x Sep 23 '14 at 13:38
2

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.

Will
  • 19,789
  • 10
  • 43
  • 45
  • 1
    In fact, when you set those attibutes to "@android:color/transparent", there is no visual indication whether the element is being selected or not. – Nappy Oct 15 '12 at 15:51
0

Cant you do with a RadioButton thats selected when user touches a Row on the List?

Following is a excerpt from my own code.

// Create an adapter with a simple list item with single choice.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, categories);

// Set the adapter to your list
listView.setAdapter(adapter);

Cheers!

SSG
  • 192
  • 2
  • 12