2

My initial question was posted here:

ListView for messaging app shows wrong listItem layout after scrolling

But I am a bit confused since there are two answers that were upvoted and I'd like to use both of them. First, I think it is a safe assumption that I should use the getItemViewType method to help me with performance. After that though, should I still use the viewHolder pattern as described in Googles documentation on Making ListView Scrolling Smooth?

If I do use the ViewHolder code, do I incorporate it into 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;
     }
Community
  • 1
  • 1
c_idle
  • 1,448
  • 4
  • 22
  • 40

1 Answers1

0

//Typical getView

public View getView(int position, View containerRow, ViewGroup parent) {

if (containerRow == null) {

    //One time inflate
    containerRow = inflater.inflate(R.layout.yourLayout, parent, false);

    //instantiate all view from layout in levent relevent object.

    //One time instantiation of viewholder
    viewHolder = new ViewHolder();

    viewHolder.someview = viewFromLayou;

    //finally
    containerRow.setTag(viewHolder);
}
else{
    viewHolder = (ViewHolder) containerRow.getTag();
}
}

//In your case it should be

public View getView(int position, View containerRow, ViewGroup parent) {

if (containerRow == null) {

    //One time inflate
    containerRow = inflater.inflate(R.layout.yourLayout, parent, false);

    //instantiate all view from layout in levent relevent object.

    //One time instantiation of viewholder
    if(inbound)
        inBoundViewHolder = new ViewHolder();
    else            
        outBoundViewHolder = new ViewHolder();

    viewHolder.someview = viewFromLayou;

    //finally
    containerRow.setTag(viewHolder);
}
else{
    viewHolder = (ViewHolder) containerRow.getTag();
}
}
user2095470
  • 356
  • 1
  • 6
  • 23