我發現的GridView導向上顯影劑機器人:https://developer.android.com/guide/topics/ui/layout/gridview.html的Android BaseAdapter 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;
}
「在getView()方法的結束時,傳遞給該方法的位置整數被用於選擇從mThumbIds陣列的圖像,它被設置爲ImageView的圖像資源。「
我對此感到困惑。
爲什麼我們每次都需要調用setImageResource而不是僅在convertView爲null時調用它?不應該convertView如果不是空已經有圖像資源集,像填充,規模設置等
// 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);
imageView.setImageResource(mThumbIds[position]);
} else {
imageView = (ImageView) convertView;
}
return imageView;
}