Focus Control in ListView

Context: I want to have a ListView that won't receive focus (for example, won't highlight a row when the user touches it). However, each row widget has its own OnClickListener. Here is what I indicate in the xml layout:

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

ListView still behaves exactly the same. Can someone explain

  • Relationship between the three
  • What is the right way to create a ListView that does not get focus?

TIA.

+3
source share
3 answers

, . , . , ListView, - int , . , ClickListener. . onListItemClick ListView.

, android:listSelector android:background -, , , .

android:listSelector="#8fff" , .

, , . , .

+2

xml, . , , , .

setContentView...

myListView.setFocusableInTouchMode(false);

, , , .

    myListView.setOnFocusChangeListener(new OnFocusChangeListener(){

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

    myListView.setClickable(true);
+6

Can't you use the RadioButton, which is selected when the user touches a line in the list?

Below is a snippet of 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);

Hurrah!

0
source

Source: https://habr.com/ru/post/1703610/


All Articles