views:

68

answers:

1

Hi, in my listview I have items with different layouts, in fact they use one sub-layout few times. I cannot use getViewTypeCount() and getItemViewType(), because I don't know how many times the sublayout will be used. Is it possible to optimize somehow getView() method and use it's convertView parameter, or do I have to inflate view each time?

+1  A: 

Since you know how many types of layout you would have - it's possible to use those methods.

getViewTypeCount() - this methods returns information how many types of rows do you have in your list

getItemViewType(int position) - returns information which layout type you should use based on position

Then you inflate layout only if it's null and determine type using getItemViewType.

Look at this tutorial for further information.

UPDATE:

To achieve some optimizations in structure that you've described in comment I would suggest:

  • Storing views in object called ViewHolder. It would increase speed because you won't have to call findViewById() every time in getView method. See List14 in API demos.
  • Create one generic layout that will conform all combinations of properties and hide some elements if current position doesn't have it.

I hope that will help you. If you could provide some XML stub with your data structure and information how exactly you want to map it into row, I would be able to give you more precise advise

pixel
thanks for input, but as I said, I don't know how many different layouts will be used in the listview, that's why I cannot override getViewTypeCount()
Can you provide more details, why cant you predict position and number of the layouts. This is quite wierd. The the anwser provided by pixel is the only way to inform view recycler about curently needed layout.
darbat
I send http request to the web service to obtain xml response, where there are, e.g. just <item> nodes with few property child nodes followed by various number of <sub-item> children, so I created two xml layouts, one for the item, and one for sub-item, in the item layout I have create few views for its properties as well a linear layout, in which I add these sub-items
Why don't you take a look to ExpandableListView ?
fedj