2
我最初的問題是張貼在這裏:如果我使用不同的ViewItemTypes,我應該使用viewHolders嗎?
ListView for messaging app shows wrong listItem layout after scrolling
但我有點糊塗了,因爲有被upvoted這兩個答案,我想使用這兩者。首先,我認爲這是一個安全的假設,我應該使用getItemViewType
方法來幫助我提高性能。但在此之後,我是否仍然使用Making ListView Scrolling Smooth上的Google文檔中描述的viewHolder模式?
如果我使用ViewHolder代碼,是否將它合併到getView中?
static class ViewHolder {
TextView text;
TextView timestamp;
ImageView icon;
ProgressBar progress;
int position;
}
static public enum LAYOUT_TYPE {
INBOUND,
OUTBOUND
}
@Override
public int getViewTypeCount() {
return LAYOUT_TYPE.values().length;
}
@Override
public int getItemViewType (int position) {
if (messages.get(position).isOutbound())
return LAYOUT_TYPE.OUTBOUND.ordinal();
else
return LAYOUT_TYPE.INBOUND.ordinal();
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
LAYOUT_TYPE itemType = LAYOUT_TYPE.values()[getItemViewType(position)];
... (code until inflater)
switch (itemType){
case INBOUND:
convertview = /inflate & configure inbound layout
ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
convertView.setTag(holder);
break;
case OUTBOUND:
convertview = /inflate & configure outbound layout
ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
convertView.setTag(holder);
break;
}