5

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?

Ratata Tata
  • 2,781
  • 1
  • 34
  • 49

2 Answers2

1

You have just TextView according to this:

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

Community
  • 1
  • 1
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
0

The setSelection, setItemChecked, performItemClick, extending ListView or whatever you are thinking to do, is not possible. The AbsListView uses a private method to do it called positionSelector. That is only called inside others private methods and checked to make sure there was an user interaction before calling positionSelector.

The only workaround you can do is force a touch event:

View temp = navMenu.getChildAt( 1 );
navMenu.onTouchEvent( MotionEvent.obtain( SystemClock.uptimeMillis(), 
    SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, temp.getX(), temp.getY(), 0 ) );
Ratata Tata
  • 2,781
  • 1
  • 34
  • 49