Question

I have to use different layouts for each row, and i am following a simple approach and i want to confirm whether i am going in correct direction.

1) I am keeping a Weak reference of views for reusing.

    private WeakReference<View> mLocationView;
    private WeakReference<View> mGallaryView;
    ..

2) And then while loading i am checking

    if (mGallaryView == null || mGallaryView.get() == null) {....

       //Inflating XML

        mGallaryView = new WeakReference<View>(convertView);

    }else {
        convertView = mGallaryView.get();
    }

Am i doing it correctly or there is any other way?

Was it helpful?

Solution

Am i doing it correctly

No.

there is any other way?

Step #1: Override getViewTypeCount() in your ListAdapter and return how many different types of rows you have (e.g., 2). Let's call this value N.

Step #2: Override getItemViewType() in your ListAdapter and return a number, from 0 to N-1, indicating the particular type of row to use for this position.

Step #3: In your getView() method, just return the right row layout for the given position. The convertView will either be null (so you create a new one) or it will be a row of the right type (so you just use it).

See also:

and countless other sources.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top