Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

performItemClick in ListView doesn't highlight item

Tags:

android

Then I have a ListView set as:

<ListView
    android:id="@+id/list_menu_nav"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    android:listSelector="#fff">
</ListView>

The adapter is set as:

navMenu = (ListView) findViewById( R.id.list_menu_nav );
navMenu.setAdapter( new ArrayAdapter<String>( this,
                    android.R.layout.simple_list_item_1,
                    menuList ) );

When I touch in an item, it gets a white background (as I set). But, when I performClick to a item as below, it is selected but doesn't get the white background.

navMenu.performItemClick( navMenu.getChildAt( 1 ), 1, navMenu.getAdapter().getItemId( 1 ) );

I know it is selected because everything else works as expected (listener called, getCheckedItemPosition returns correct value).

What is the correct approach in this case? Substitute the layout for a custom one and make by myself the highlightening or is there a way using the same default layout?

like image 723
Ratata Tata Avatar asked Nov 12 '22 17:11

Ratata Tata


1 Answers

You have just TextView according to this:

  • https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/simple_list_item_1.xml

So this behaviour is expected. You have to create own layout for item element to customise selected state. Take a look here:

  • Android - Textview change color on changing of state
  • TextView state_pressed/state_focused/state_selected style change
like image 75
Eugen Martynov Avatar answered Nov 15 '22 13:11

Eugen Martynov