Android 的 ViewHolder 相信大家都有接觸過,
但是那個程式碼實在很醜很煩,
找到了一種比較簡潔的寫法,
分享給大家,
1 | public class ViewHolder { |
useage1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context)
.inflate(R.layout.banana_phone, parent, false);
}
ImageView bananaView = ViewHolder.get(convertView, R.id.banana);
TextView phoneView = ViewHolder.get(convertView, R.id.phone);
BananaPhone bananaPhone = getItem(position);
phoneView.setText(bananaPhone.getPhone());
bananaView.setImageResource(bananaPhone.getBanana());
return convertView;
}