First, I know there are a lot of questions about the same theme, but I try a lot of solutions and I don't achieve it works.
I have an app with a DrawerLayout
and I want to change the item selected by code, the option change but the item is not highlight and I don't know why. It muss work in versions >= 2.3 (api 9)
If I click manually it works
The layout of the items are: I put android:focusableInTouchMode
to false because I read that the selected item don't work if it is enabled.
I put too a background.
The choiceMode: drawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llItem"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/list_selector_holo_light"
android:focusableInTouchMode="false">
<ImageView
android:id="@+id/iconoMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:paddingLeft="5dp"
android:paddingRight="12dp"/>
<TextView
android:id="@+id/etTitulo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/menu_item"/>
</LinearLayout>
The xml file to the background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="@android:color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_light" />
<item android:state_focused="true" android:drawable="@drawable/list_focused_holo" />
<item android:state_focused="false" android:state_selected="true" android:drawable="@drawable/list_pressed_holo_light"/>
</selector>
Then I emule the click on the item with this code (home is the selected option):
drawerList.performItemClick(drawerList, home, drawerList.getItemIdAtPosition(home));
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
changeOption(position, true);
view.setSelected(true);
}
});
private void changeOption(int position, Boolean cerrar) {
fragment = new Home();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
tituloSeccion = ((MenuLateralItem) opcionesMenu[position])
.getTitulo();
getSupportActionBar().setTitle(tituloSeccion);
if ((cerrar) && (!isDrawerLocked)) {
drawerLayout.closeDrawer(drawerList);
}
}
Thank you! And sorry for my english.