Android:ListView 多项选择不工作
Android: ListView Multiple Choice not working
当用户在列表视图中选择 item/row 时,它应该是 selected/highlighted 某种颜色(例如,背景颜色应该改变)。
在我的MyActivity.xml:
<LinearLayout
android:id="@+id/linlay1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/myblackimage" >
...
<ListView
android:id="@+id/listview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:drawSelectorOnTop="true"
android:listSelector="@drawable/list_item_selector">
</ListView>
...
list_item_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@android:color/darker_gray" android:state_pressed="true"/>
<item android:drawable="@android:color/background_light" android:state_activated="true"/>
<item android:drawable="@android:color/darker_gray"/>
</selector>
item.xml 这在列表视图中使用了 item/row(我认为这是应该根据选择更改背景颜色的内容)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="itemPicked"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/item_orderno"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textAppearance="@android:style/TextAppearance.Medium"
/>
<TextView
android:id="@+id/item_ordername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textAppearance="@android:style/TextAppearance.Small"/>
</LinearLayout>
MyActivity.java :
private MyAdapter myAdt;
private ArrayList myArrayList;
...
protected void onCreate(Bundle savedInstanceState) {
ListView myListView1 = (ListView) findViewById(R.id.listView1);
...
myAdt = new MyAdapter(this, myArrayList);
myListView1.setAdapter(myAdt);
...
}
当我在列表视图中单击 item/row 时,该行根本没有改变颜色。
我在这里做错了什么?
我想问题出在 list_item_selector.xml
请注意,默认状态和按下状态具有相同的可绘制背景
<item android:drawable="@android:color/darker_gray"/>
<item android:drawable="@android:color/darker_gray" android:state_pressed="true"/>
使它们不同,并添加一个选定的状态,像这样
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@android:color/darker_gray" android:state_pressed="true"/>
<item android:drawable="@android:color/darker_gray" android:state_selected="true"/>
<item android:drawable="@android:color/background_light" android:state_activated="true"/>
<item android:drawable="@android:color/background_light"/>
</selector>
以防万一这不起作用删除 state_activated
除上述所有内容外,从 my_activity.xml 中删除此行(还要确保 xml nam 中的所有小写字母)
android:listSelector="@drawable/list_item_selector"
并将此行添加到 item.xml
的根项目
android:background="@drawable/list_item_selector"
看起来像这样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="itemPicked"
android:orientation="vertical"
android:padding="5dp"
android:background="@drawable/list_item_selector" >
当用户在列表视图中选择 item/row 时,它应该是 selected/highlighted 某种颜色(例如,背景颜色应该改变)。
在我的MyActivity.xml:
<LinearLayout
android:id="@+id/linlay1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/myblackimage" >
...
<ListView
android:id="@+id/listview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:drawSelectorOnTop="true"
android:listSelector="@drawable/list_item_selector">
</ListView>
...
list_item_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@android:color/darker_gray" android:state_pressed="true"/>
<item android:drawable="@android:color/background_light" android:state_activated="true"/>
<item android:drawable="@android:color/darker_gray"/>
</selector>
item.xml 这在列表视图中使用了 item/row(我认为这是应该根据选择更改背景颜色的内容)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="itemPicked"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/item_orderno"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textAppearance="@android:style/TextAppearance.Medium"
/>
<TextView
android:id="@+id/item_ordername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textAppearance="@android:style/TextAppearance.Small"/>
</LinearLayout>
MyActivity.java :
private MyAdapter myAdt;
private ArrayList myArrayList;
...
protected void onCreate(Bundle savedInstanceState) {
ListView myListView1 = (ListView) findViewById(R.id.listView1);
...
myAdt = new MyAdapter(this, myArrayList);
myListView1.setAdapter(myAdt);
...
}
当我在列表视图中单击 item/row 时,该行根本没有改变颜色。
我在这里做错了什么?
我想问题出在 list_item_selector.xml 请注意,默认状态和按下状态具有相同的可绘制背景
<item android:drawable="@android:color/darker_gray"/>
<item android:drawable="@android:color/darker_gray" android:state_pressed="true"/>
使它们不同,并添加一个选定的状态,像这样
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@android:color/darker_gray" android:state_pressed="true"/>
<item android:drawable="@android:color/darker_gray" android:state_selected="true"/>
<item android:drawable="@android:color/background_light" android:state_activated="true"/>
<item android:drawable="@android:color/background_light"/>
</selector>
以防万一这不起作用删除 state_activated
除上述所有内容外,从 my_activity.xml 中删除此行(还要确保 xml nam 中的所有小写字母)
android:listSelector="@drawable/list_item_selector"
并将此行添加到 item.xml
的根项目android:background="@drawable/list_item_selector"
看起来像这样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="itemPicked"
android:orientation="vertical"
android:padding="5dp"
android:background="@drawable/list_item_selector" >