Abdul Waheed
Abdul Waheed

Reputation: 4678

How to manage multiple layouts for single row of List View in Android?

I have got a situation.I am making chat application, on that I have one sender side and one receiver side. All the chats is shown in a List View individual rows.On get view lets suppose I have 5 sender messges and 5 receiver messages (including images) how will I handle the case.For sending side I have a layout (I am inflating using holder view pattern) it contains progress bar to show status image uploading on the other hand I am inflating another layout but on that I don't have any progress bar.In this case How recyles works?I mean last item(10th item visible to screen) is image that belongs to reciver side and it has no progress bar but as soon as scroll for 11th item recyle works and 11 th item is from sender side and it contains progress bar.Further more for system messages there is a third layout (to show status today,yesterday) and this third alot has just single text view what would happen when it gets reccyle for that item that contains image(when this layout does not have any image view) Should I use single row for all this and show/hide accordin to data Or is there any better approach available?

Upvotes: 1

Views: 423

Answers (2)

human123
human123

Reputation: 295

You need to use

getViewTypeCount()- Returns the number of types of Views that will be created by getView(int, View, ViewGroup).

getItemViewType(int position) - Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.

@Override
public int getItemViewType(int pos) {
    return pos % 2;
}

@Override
public int getViewTypeCount() {
    return 2; 
}

Then update getView to handle 2 types of layouts

public View getView(int pos, View convertView, ViewGroup parent) {
    ..............................................................
    ..............................................................
    if (convertView == null) {
        inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        if (getItemViewType(pos) == 0) {
            convertView = inflater.inflate(R.layout.layout_type_one, null);
        } else {
            convertView = inflater.inflate(R.layout.layout_type_two, null);
        }
        ..............................................................
        ..............................................................

    }
    ...................................................................
    ...................................................................
} 

Upvotes: 1

Anjali
Anjali

Reputation: 2535

You can create a type field in your model class and inflate your View according to this type only.

your getView method will look like this:

           if (convertView == null) {
                if (model.getType() == 0) {
                    convertView = mInflater.inflate(R.layout.row_sent_message,
                            null);
                } else if (model.getView() == 1) {
                    convertView = mInflater.inflate(R.layout.row_sent_message,
                            null);
                } else if (model.getView() == 2) {
                    convertView = mInflater.inflate(R.layout.row_other, null);
                }
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

And your ViewHolder Class should be like this:

private class ViewHolder {


        public ViewHolder(View rowView, int rowType) {
            // TODO Auto-generated constructor stub

            // According to your rowType find id of your view here 

        }
    }

Upvotes: 1

Related Questions