まず、同じテーマについて多くの質問があることは知っていますが、多くの解決策を試してみましたが、うまくいきません。
のアプリがあり、DrawerLayout
コードで選択した項目を変更したいのですが、オプションを変更しましたが、項目が強調表示されず、理由がわかりません。バージョン>= 2.3(api 9)で動作する必要があります手動でクリックすると動作します
アイテムのレイアウトは次のとおりです。android:focusableInTouchMode
選択したアイテムが有効になっていると機能しないことを読んだので、falseにしました。背景も入れました。
選択モード: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);
}
}
ありがとうございました!そして私の英語でごめんなさい。