Here is the problem i am facing. I am developing a custom list view in which a single list view item is consisted of multiple text and image views. But depending on the item properties it sometimes needs to have 4 TextViews sometimes 1, sometimes 6.... the number is dynamic and i am having trouble figuring out how to developed a proper view holder pattern that will work.
I can not solve the problem by creating a number of TextViews and hiding them and displaying when necessary, since i dont know how many of them are necessary per item.
And inflating them dynamically and adding to the layout every time in getView is disastrous to say the least :(
Is it a viable solution to have an array list of Views in the holder and then work something out?
I'd recommend using the adapters view types: getItemViewType() and getViewTypeCount() The number of view types depends on how many permutations are needed to represent all the varying ways an individual list item will look.
So if there are 4 different UI looks, then you'd have 4 different view types. You'd then have a ViewHolder representing each type. Then in the
getView()
, you just detect which type is represented by the position and inflate/populate as needed. You won't need to worry about hiding different portions of the UI to change up the look. The adapter will ensure you are only given a recycled view for the proper type.For a nice detailed explanation of using the view type, check out this post.