导航抽屉突出显示所选项目不起作用

2024-04-30

我试图突出显示选定的导航抽屉项目,但它不起作用。它仅在按下项目时突出显示,但在选择项目后不会保持突出显示。

我有以下代码:

列表视图:

<ListView
    android:id="@+id/drawer_listview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:choiceMode="singleChoice"
    android:divider="@color/drawer_divider"
    android:dividerHeight="@dimen/drawer_divider_height"
    android:listSelector="@drawable/list_selector_holo_light" />

选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" android:state_window_focused="false"/>
<item android:drawable="@drawable/list_selector_disabled_holo_light" android:state_enabled="false" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/list_selector_disabled_holo_light" android:state_enabled="false" android:state_focused="true"/>
<item android:drawable="@drawable/list_selector_background_transition_holo_light" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/list_selector_background_transition_holo_light" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/list_activated_holo" android:state_activated="true" />
<item android:drawable="@drawable/list_focused_holo" android:state_focused="true"/>

可绘制文件是使用以下命令生成的 9 个补丁文件Android 全息颜色 http://android-holo-colors.com/.

在我的活动中:

  mListView.setAdapter(mAdapter);
  mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  mListView.setItemChecked(1, true); // Testing
  mListView.setSelection(1); // Testing

据我所知,state_activated="true"在选择器中是当 listView 项目被选中/选择时。但它不起作用。

Edit:

I set android:background="@drawable/list_selector_holo_light"对于行布局,现在它正在工作,但我仍然不知道为什么 listSelector 不工作。


您使用的是哪个版本的 Android?

我认为 state_activated 适用于 API 级别 11 及更高级别。

我经历过这种情况,为了处理 Pre Honeycomb,我为 ListView 创建了一个定制的适配器,并在以下代码中getView方法:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
     if (mListView.isItemChecked(position)) {
           holder.tvDrawerItem.setBackgroundColor(R.drawable.list_activated_holo);
     } else {
           holder.tvDrawerItem.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
     }
}

附加物:使用 Android 支持库 v4 支持 Pre HoneyComb https://stackoverflow.com/questions/17629696/android-api-specific-layout-attribute.

如果您想支持 Android 4+,只需查看 Android 开发人员示例:http://developer.android.com/training/implementing-navigation/nav-drawer.html http://developer.android.com/training/implementing-navigation/nav-drawer.html并检查抽屉列表布局。activatedBackgroundIndicator是你所需要的:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

导航抽屉突出显示所选项目不起作用 的相关文章

随机推荐