0

我的抽屉布局:

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

我现在唯一的问题是按下状态颜色没有填充列表中的整个项目行: 在此处输入图像描述

4

1 回答 1

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">
    ...
于 2013-11-02T11:19:29.500 回答