1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
publicView getView(intposition, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if(convertView ==null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text,null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder =newViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}else{
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position &1) ==1? mIcon1 : mIcon2);
returnconvertView;
}
staticclassViewHolder {
TextView text;
ImageView icon;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<?xml version="1.0"encoding="utf-8"?>
<selector xmlns:android="[url= http://schemas.android.com/apk/res/android] http://schemas.android.com/apk/res/android[/url]">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/ltgray"/>
<item android:state_pressed="true"
android:drawable="@color/red"/>
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/red"/>
</selector>
mail_unread_bg.xml
<?xml version="1.0"encoding="utf-8"?>
<selector xmlns:android="[url= http://schemas.android.com/apk/res/android] http://schemas.android.com/apk/res/android[/url]">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/white"/>
<item android:state_pressed="true"
android:drawable="@color/red"/>
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/red"/>
</selector>
|
1
2
3
4
|
ifunread
convertView.setBackgroundResource(R.drawable.mail_unread_bg);
else
convertView.setBackgroundResource(R.drawable.mail_read_bg);
|