Android: EfficientAdapter with two different Views

后端 未结 2 868
逝去的感伤
逝去的感伤 2020-12-14 04:35

I\'m using an extended version of BaseAdapter based on the EfficientAdapter example from the SDK demo samples.

My data is basically an object (ListPlaces

2条回答
  •  青春惊慌失措
    2020-12-14 05:16

    Thanks to the hint with getViewTypeCount() and getItemViewType() it works perfectly now.

    Implementing these two methods was very simple:

    @Override
    public int getViewTypeCount() {
        return 2;
    }
    
    @Override
    public int getItemViewType(int position) {
    if(listPlaces.getValues().get(position).separator >= 0)
        return 0;
    else
        return 1;
    }
    

    As commonsware mentioned in his answer this way Android will maintain different object pools for different list items, which also means you can remove the check for listRow_previous in my example and change the if (convertView == null || (listRow != listRow_previous)) to if (convertView == null) only.

提交回复
热议问题