抽屉式导航栏高亮选中的项目不能正常工作 [英] Navigation Drawer highlight selected item not working
问题描述
我想强调所选导航抽屉里的项目,但它不工作。它不仅突出在pressing的项目,但该项目被选中后并没有继续强调。
I'm trying to highlight the selected nav drawer item but it doesn't work. it only highlights on pressing the items but does not remain highlighted after the item is selected.
我有以下的code:
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的全息颜色产生的9补丁文件。
在我的活动:
mListView.setAdapter(mAdapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setItemChecked(1, true); // Testing
mListView.setSelection(1); // Testing
据我所知, state_activated =真正的
的选择是当ListView项被选中/选择。但它不工作
As far as I know, the state_activated="true"
in the selector is when the listView item is checked/selected. but it doesn't work.
我设置安卓背景=@可绘制/ list_selector_holo_light
的行布局,现在它的工作,但我仍然不知道为什么listSelector不工作。
I set android:background="@drawable/list_selector_holo_light"
for the row layout and now it is working, but I still have no idea why listSelector is not working.
推荐答案
哪个版本的Android您使用的是?
Which version of Android are you using?
我觉得state_activated正在为API级别11或更高。
I think state_activated is working for API level 11 and higher.
我也经历过这一点,以处理pre蜂窝,我创建了一个定制的适配器ListView和具有以下code。在 getView
方法:
I have experienced this and in order to handle Pre Honeycomb, I create a customized Adapter for ListView and have following code in getView
method:
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蜂窝支持。
Addenda: support for Pre HoneyComb using android support Library v4.
如果你想支持Android 4+,只是检查Android开发样品:的 http://developer.android.com/training/implementing-navigation/nav-drawer.html 并检查drawer_list_layout。 activatedBackgroundIndicator
是你所需要的:
If you suppose to support Android 4+, just check Android developer Sample : http://developer.android.com/training/implementing-navigation/nav-drawer.html and check drawer_list_layout. activatedBackgroundIndicator
is what you need:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
...
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
这篇关于抽屉式导航栏高亮选中的项目不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!