1

There is a problem with item selection in custom ListView. There is no checkboxes, so no need in checkable=false. I've tried setup android:focusable="false" and android:focusableInTouchMode="false". Tried "Draw Selector On Top option". I can see standard touch animation, but items not highlighted.

My ListView definition (activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</LinearLayout>

ListView creating in onCreate of main activity (MainActivity.java):

...
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        view = (ListView) findViewById(R.id.listview);
        view.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        list = new ArrayList<String>();

        currentDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();

        adapter = new ArrayAdapter<String>(this, R.layout.listviewitem, R.id.firstLine, list);
        view.setAdapter(adapter);

        updateFileList(currentDirectory);

        view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View parent_view, int position, long id) {
              String listItem = (String) view.getItemAtPosition(position);
              Log.d("Hello", "file: " + listItem);

              view.setSelected(true);

              File tempFile = new File(currentDirectory + File.separator + listItem);

              if (tempFile.isDirectory()) {
                  currentDirectory = currentDirectory + File.separator + listItem;
                  updateFileList(currentDirectory);
              }
           }
        });
    }
...

R.layout.listviewitem consists of ImageView and two TextViews (listviewitem.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/menu_item_background_selector"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Description"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:text="Example application"
        android:textSize="16sp" />

</RelativeLayout> 

Where selector menu_item_background_selector (menu_item_background_selector.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true"></item>
    <solid android:color="#bfced4"></solid>
</selector>
Alan Kazbekov
  • 1,105
  • 2
  • 12
  • 31

1 Answers1

0

the selector is wrong. The item should wrap the state and the color. And you should have a default item. E.g.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:drawable="#bfced4" android:state_activated="true" />
    <item android:drawable="@color/default_color" />
</selector>

Android will check the state from the top. So if android:state_activated is true, the corresponding color will be picked up. If it is false, it checks the next on the selector, until it reach the last one

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Did you mean: `` ``? Just tried with same result. – Alan Kazbekov Jun 26 '15 at 14:47
  • Are you sure that activated is the state you need? Maybe you want state_pressed ? – Blackbelt Jun 26 '15 at 14:51
  • I've tried state_pressed and state_selected. I want items stay highlighted in selected state. I use http://stackoverflow.com/a/10791326/4449456 as example and check all related options from http://developer.android.com/guide/topics/resources/color-list-resource.html – Alan Kazbekov Jun 26 '15 at 15:03