這正是你所說的一種回收形式。
膨脹的佈局需要大量的內存和大量的時間,所以爲了提高效率,系統會傳遞給你剛剛離開屏幕的內容,並且你可以簡單地更新它的文本和圖像並將它們返回給UI。因此,例如,如果您的列表視圖在其列表中顯示6個項目(由於它的高度),它將只膨脹6個項目,並且在滾動期間它只是繼續回收它們。
有一些額外的優化技巧,你應該使用,我敢肯定,評論者發佈的視頻鏈接將解釋他們。
編輯
這個例子是商店物品的ArrayAdapter,但你可以把它到任何你所需要的。 適配器執行UI和數據之間的匹配和分隔層。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = newView();
// Store is the type of this ArrayAdapter
Store store = getItem(position);
Holder h = (Holder) convertView.getTag();
// And here I get the data and address them to the UI
// as you can see, if the convertView is not null,
// I'm not creating a new one, I'm just changing text, images, etc
h.storeName.setText(store.getStoreName());
h.address.setText(store.getAddressLine1());
h.postcode.setText(store.getPostCode());
h.distance.setText(store.getDistance());
return convertView;
}
// I like to separate in a different method when the convertView is null
// but that's me being organisation obsessive
// but it also makes easy to see which methods are only being called the 1st time
private View newView() {
LayoutInflater inf = LayoutInflater.from(getContext());
View v = inf.inflate(R.layout.map_result_list, null);
Holder h = new Holder();
// here we store that holder inside the view itself
v.setTag(h);
// and only call those findById on this first start
h.storeName = (TextView) v.findViewById(R.id.txtLine1);
h.address = (TextView) v.findViewById(R.id.txtLine2);
h.postcode = (TextView) v.findViewById(R.id.txtLine3);
h.distance = (TextView) v.findViewById(R.id.txtDistance);
return v;
}
// this class is here just to hold reference to the UI elements
// findViewById is a lengthy operation so this is one of the optimisations
private class Holder {
TextView storeName;
TextView address;
TextView postcode;
TextView distance;
}
http://www.youtube.com/watch?v=wDBM6wVEO70。看看鏈接。你的問題可能已經回答了。 – Raghunandan
如果列表中有三個項目,並且屏幕上有三個ListView中的空間,getView()將被調用三次,並使用空視圖來創建這三行。您無法回收目前正在使用的行。 – Raghunandan
好吧,我明白了,是基本上將視圖放到列表中的視圖v = vi.inflate(.layout.inbox_row,null)的佈局充氣器? – Javacadabra