[英]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.