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

9

我希望能够突出显示所选的导航抽屉项,但它不起作用。只有在按下选项时才会高亮显示,但选定项后不会保持高亮。

我有以下代码:

ListView:

<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"/>

可绘制对象是由Android Holo Colors生成的九宫格文件。

在我的活动中:

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

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

编辑:

我为行布局设置了android:background="@drawable/list_selector_holo_light",现在它可以工作了,但我仍然不知道为什么listSelector不起作用。

3个回答

3

你正在使用哪个版本的Android?

我认为状态激活对API级别11及以上版本有效。

我已经体验过这种情况,并且为了处理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 布局属性
如果你想要支持Android 4+,只需查看Android开发人员示例:http://developer.android.com/training/implementing-navigation/nav-drawer.html并检查drawer_list_layout。 activatedBackgroundIndicator是你所需要的内容。
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

你能分享一下设置 NavigationDrawer 列表点击监听器的代码吗?(mDrawerList.setOnItemClickListener)? - Ali

0
drawerList.setItemChecked(currentPosition,true);

其中drawerList是您的导航栏列表


0
你设置了列表项的背景颜色吗?
如果是这样,你尝试过android:drawSelectorOnTop =“true”吗?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接