ListView无法设置所选的背景颜色

时间:2015-03-05 00:59:44

标签: java android listview android-5.0-lollipop

我正在尝试在选择时更改列表视图项的背景颜色。

在MainActivity中:

leftDrawerList = (ListView) findViewById(R.id.left_drawer);
    navDrawerItems = new ArrayList<NavDrawerItem>();
    // adding nav drawer items to array
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1), true, "99+"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));

    // Recycle the typed array
    navMenuIcons.recycle();

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),navDrawerItems);
    leftDrawerList.setAdapter(adapter);

    leftDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    leftDrawerList.setItemChecked(0, true);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

我的选择器xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
  <item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
  <item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>

</selector>

我的MainActivity布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<include layout="@layout/toolbar" />


<android.support.v4.widget.DrawerLayout
    android:layout_width="match_parent"
    android:id="@+id/drawerLayout"
    android:layout_height="match_parent">

    <!-- activity view -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:background="#fff"
        android:layout_height="match_parent">

        <TextView
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:textColor="#000"
            android:text="Activity Content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <!-- navigation drawer -->
    <RelativeLayout
        android:layout_gravity="left|start"
        android:layout_width="match_parent"
        android:background="#fff"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#FFFFFFFF"
            android:background="#FFFFFFFF"
            android:listSelector="@drawable/list_selector"
            android:dividerHeight="0dp" />
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

我的列表项xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">

    

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/icon"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@color/list_item_title"
    android:gravity="center_vertical"
    android:paddingRight="40dp"/>

<TextView android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/counter_bg"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="8dp"
    android:textColor="@color/counter_text_color"/>

然而,不知何故,选择项目时颜色不会改变!

我确保我的颜色定义正确。

1 个答案:

答案 0 :(得分:0)

尝试为android:drawSelectorOnTop=true设置ListView。像这样:

<ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#FFFFFFFF"
        android:background="#FFFFFFFF"
        android:listSelector="@drawable/list_selector"
        android:drawSelectorOnTop="true"
        android:dividerHeight="0dp" />