1

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?

JanBo
  • 2,925
  • 3
  • 23
  • 32

1 Answers1

0

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.

Community
  • 1
  • 1
Ifrit
  • 6,791
  • 8
  • 50
  • 79
  • 1
    The sollution cant be applied to my case since i dont have a static number of view types...over a hundred combinations come to mind... I have implemented arraylists of views in viewholders and with some debugging works like a charm. – JanBo Nov 12 '14 at 14:46
  • Wow. That's a lot of combos. – Ifrit Nov 12 '14 at 15:01