views:

52

answers:

2

I have a listview and i inflate the first position with a layout and every other position i do another inflation with another layout. I can do that by checking every time in getView, but as you can imagine this is not very efficient.

Can you share me your thoughts to get that result by having this pattern? (This don't get me the correct result(cause after 4-5 positions, depending of the number of positions inflated the first time, i get the layout.inside_list_2 on other positions too.)

if (convertView == null) {
  if (position == 0)
   convertView = mInflater.inflate(R.layout.inside_list_2,
                            parent, false);
    else
    convertView = mInflater.inflate(R.layout.inside_list, null);
  }
else{
        holder=(ViewHolder)convertView.getTag();
}
+1  A: 

Your problem has to do with the fact that the Views in a ListView are reused as you scroll. This is done for performance reasons.

Thus, in getView you cannot rely on convertView being in any given initial state. You need to check the view each time to make sure that it is the right one.

However, in your case you might want to look into using a header view.

Mayra
header view was exactly what i wanted.
weakwire
@Mayra - if you use `getItemViewType` and `getViewTypeCount` properly, you can be sure the convertView has valid type (or is null) - see my answer
tomash
+1  A: 

Here is answer for similar question - it includes pointer to ViewHolder pattern which improve list performance - http://stackoverflow.com/questions/2081565/android-viewholder-pattern-and-different-types-of-rows

tomash