[英]Android - States for different imageview resources as a listview items
我已经定制了一个列表视图项。 每个项目列表都有一个imageview。
imageview的类型由类别变量定义,我将其保存在数据库中。
因此,假设列表视图中包含三个类别的三个项目-蓝色,红色和黑色。
所以CursorAdapter的bindView如下-
public void bindView(View view, Context context, Cursor cursor) {
String cat = cursor.getString(cursor.getColumnIndex(Constans.CATEGORY));
ImageView iv = (ImageView) view.findViewById(R.id.imageView);
if (cat.equalsIgnoreCase("black")) {
iv.setImageResource(R.drawable.black_un);
}
if (cat.equalsIgnoreCase("blue")) {
iv.setImageResource(R.drawable.blue_un);
}
if (cat.equalsIgnoreCase("red")) {
iv.setImageResource(R.drawable.red_un);
}
}
现在,我想做的就是按下listviewitem时-我希望imageview将显示相关的“按下”图像-
我的意思是,现在imageview显示的是非按下状态的图像-但是当用户按listview项目时,我希望它会显示“按下”的图像-然后imageview会再次显示非按下的图像-只是就像用按钮一样。
有什么想法我该怎么做? 请注意,每个原始列表视图都有不同的图像-当然取决于类别。
为此使用选择器:
selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/not_pressed_image" android:state_pressed="false"></item>
<item android:drawable="@drawable/pressed_image" android:state_pressed="true"></item>
</selector>
与其设置一个可绘制对象,不如将选择器设置为:
iv.setImageResource(R.drawable.selector);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.