如何实现类似于Google Play音乐的导航抽屉选择器

时间:2013-10-12 05:51:02

标签: android android-selector android-navigation

我希望实现一个导航抽屉选择器,它在抽屉打开时始终显示当前所选项目,即使抽屉关闭也会保留。像这样:

enter image description here

我使用过这样的东西,但是当我点击列表项时它只显示选择器。这是在我的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>

我如何实现上述实施?我需要实施哪个州?感谢

2 个答案:

答案 0 :(得分:1)

通过在NavigationAdrawer中填充我的列表的ArrayAdapter的getView()中设置颜色来解决同样的问题:

@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;
    }

答案 1 :(得分:0)

您必须使用另一个州"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>

layout.xml ,其中包含ListView

...
<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>