首先,我知道有很多关于同一主题的问题,但我尝试了很多解决方案,但都没有实现。
我有一个带有 a 的应用程序,DrawerLayout
我想更改代码选择的项目,选项更改但项目没有突出显示,我不知道为什么。它必须在 >= 2.3 (api 9) 的版本中工作 如果我手动单击它可以工作
项目的布局是:我设置android:focusableInTouchMode
为假,因为我读到如果启用所选项目,它就不起作用。我也放了背景。
选择模式: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>
xml文件到后台:
<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>
然后我使用此代码模拟对项目的单击(主页是选定的选项):
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);
}
}
谢谢!对不起我的英语。