Android - 抽屉菜单列表项在自定义适配器充气时不会改变点击颜色

时间:2013-10-31 23:21:32

标签: android

我的抽屉布局:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        />

    <!-- The navigation drawer -->
     <ListView
       android:id="@android:id/list"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="start"
       android:divider="@android:color/darker_gray"
       android:dividerHeight="0.1dp" 
       android:listSelector="@drawable/drawer_list_selector"
      />
</android.support.v4.widget.DrawerLayout>

我的列表行:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/drawer_list_item_image"
        android:contentDescription="Menu item image" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_gravity="center_vertical"
        />

    <TextView
        android:id="@+id/drawer_list_item"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:gravity="center_vertical"
        android:textColor="@android:color/black" 
        />


</LinearLayout>

然后我的自定义适配器中的getView方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View drawerRow = inflater.inflate(R.layout.drawer_row, parent, false);
    TextView drawerRowText = (TextView) drawerRow
            .findViewById(R.id.drawer_list_item);
    ImageView drawerRowImage = (ImageView) drawerRow
            .findViewById(R.id.drawer_list_item_image);

    drawerRowText.setText(drawerRowTextValues[position]);
    switch (position) {
    case 0:
        drawerRowImage.setImageResource(R.drawable.content_new_dark);
        break;
    case 1:
        drawerRowImage.setImageResource(R.drawable.location_web_site_dark);
        break;
    case 2:
        drawerRowImage.setImageResource(R.drawable.action_settings_dark);
        break;
    }

    drawerRow.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            System.out.println("drawer list item clicked...");

        }
    });

    return drawerRow;
}

我可以验证事件是否被触发只是无法在点击时看到列表项颜色更改(闪烁),而不是在使用ArrayAdapter时。

任何想法的人?

修改 好的,我通过添加listSelector得到了几乎所需的行为,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- pressed state. -->
    <item android:drawable="@color/list_row_pressed_bg"
        android:state_pressed="true"/>
</selector>

...使用以下行将listSelector添加到我的listView:

android:listSelector="@drawable/drawer_list_selector"

我现在唯一的问题是按下状态颜色不会填充列表中的整个项目行: enter image description here

1 个答案:

答案 0 :(得分:1)

更改LinearLayout的宽度以填充如下所示的行:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    ...