到目前为止,我已经看到了几个使用BaseAdapter
和ArrayAdapter<?>
. 但我仍然不完全清楚为什么应该这样。
第一个例子是继承自ArrayAdapter<?>
,这个例子用在ListView中,下面是getView方法
@override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Holder holder = null;
// Holder represents the elements of the view to use
// Here are initialized
if(null == row) {
row = LayoutInflater.from(mContext).inflate(LAYOUT_ITEM_ID, parent, false);
holder = new Holder();
holder.titleTextView = (TextView)row.findViewById(android.R.id.title);
row.setTag(holder);
} else {
holder = (Holder) row.getTag();
}
// here do operations in holder variable example
holder.titleTextView.setText("Title " + position);
return row;
}
public static class Holder {
TextView titleTextView;
}
现在在第二个示例中BaseAdapter
,在 GridView 上使用 found 这是 getView 方法
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
我的问题是:为了正确使用适配器,必须使用“Holder”静态类,这对应用程序性能和多个设备上的兼容性有什么影响(最低 API 8)。