[英]How to implement navigation drawer selector similar to Google Play Music
我希望实现一个导航抽屉选择器,它在抽屉打开时始终显示当前所选项目,即使抽屉关闭也会保留。 像这样的东西:
我使用过这样的东西,但是当我点击列表项时它只显示选择器。 这是在我的res文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/list_activated_holo" />
</selector>
我如何实现上述实现?我需要实现哪种状态? 谢谢
通过在ArrayAdapter的getView()中设置颜色来解决相同的问题,ArrayAdapter填充了NavigationDrawer中的列表:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.side_menu_list_item,
parent,
false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (mCurrentSelectedPosition == position) {
holder.title.setBackgroundResource(R.color.menu_list_separator_line);
} else {
holder.title.setBackgroundResource(R.color.app_background_light);
}
holder.title.setText(menuItems[position]);
return convertView;
}
你必须使用另一种状态"android:state_selected"
。 使用状态drawable作为列表项的背景,并为列表的listSelector使用不同的状态drawable:
list_row_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@drawable/listitem_background"
>
...
</LinearLayout>
listitem_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/android:transparent" />
<item android:drawable="@drawable/listitem_normal" />
</selector>
包含ListView
layout.xml :
...
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/listitem_selector"
/>
...
listitem_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/listitem_pressed" />
<item android:state_focused="true" android:drawable="@drawable/listitem_selected" />
</selector>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.