Michał Kawiecki Michał Kawiecki - 1 year ago 64
Android Question

how do i choose convertview to reuse?


I want to have a list with 3 significantly different layouts for list items so I make my adapter that based on type of item to display creates appropriate view.

e.g. i want to list some images, texts and numbers, each with some title.
I know that in

public View getView(int position, View convertView, ViewGroup parent)

stands for reusing no longer visible listItems views.


How can I choose the
or how can i control what i get in there?

The problem comes with different listItems views, assume I have my list starting with an image listItem, then comes a lot of text listItems and number listItems and 100 listItems later comes second image.
I assume that while scrolling down the list, (in
calls) the first
that is not null is the one with image, and since i need a view to display text listItem or number listItem I cannot use it. Then i guess that in every next
call the
is that same image listItem as in previous calls because i didn't use it before.

The unused text listItems and number listItems stuck up and when scrolling the list I need to keep creating new views, this is what i want to prevent.

Answer Source

You need to let the adapter's view recycler know that there is more than one layout and how to distinguish between the two for each row. Simply override these methods:

Here i have said for 2 different layouts. If you have more use an enum to distinguish them.

public int getItemViewType(int position) {
    // Define a way to determine which layout to use, here it's just evens and odds.
    return position % 2;

public int getViewTypeCount() {
    return 2; // Count of different layouts (Change according to your requirment)

Incorporate getItemViewType() inside getView(), like this:

if (convertView == null) {
    // You can move this line into your constructor, the inflater service won't change.
    mInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    if(getItemViewType(position) == 0)
        convertView = mInflater.inflate(R.layout.listview_item_product_1, parent,false);
        convertView = mInflater.inflate(R.layout.listview_item_product_2,parent,false);
    // etc, etc...

Watch Android's Romain Guy discuss the view recycler at Google Talks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download