[英]Android - States for different imageview resources as a listview items
I've customized a listview item. 我已经定制了一个列表视图项。 Each item list has an imageview.
每个项目列表都有一个imageview。
The type of the imageview is defined by a category variable, that I save in a DB. imageview的类型由类别变量定义,我将其保存在数据库中。
So lets's say there are three items in the listview with those three categories - blue, red and black. 因此,假设列表视图中包含三个类别的三个项目-蓝色,红色和黑色。
So the bindView of the CursorAdapter is as following - 所以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);
}
}
Now what I would like to do is when the listviewitem is being pressed - I want that the imageview will show the relevant "pressed" image - 现在,我想做的就是按下listviewitem时-我希望imageview将显示相关的“按下”图像-
what I mean is that now the imageview is showing the non pressed state image - but when the user pressing on the listview item I want that it will show the "pressed" image - and than the imageview will present the non pressed image again - just as you can do with a button. 我的意思是,现在imageview显示的是非按下状态的图像-但是当用户按listview项目时,我希望它会显示“按下”的图像-然后imageview会再次显示非按下的图像-只是就像用按钮一样。
Any ideas how can I do that? 有什么想法我该怎么做? Mind that there's a different image at each listview raw - depends of course on the category.
请注意,每个原始列表视图都有不同的图像-当然取决于类别。
use selector for this: 为此使用选择器:
selector.xml: 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>
Rather than setting one drawable set the selector like: 与其设置一个可绘制对象,不如将选择器设置为:
iv.setImageResource(R.drawable.selector);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.