Android: EfficientAdapter with two different Views

后端 未结 2 867
逝去的感伤
逝去的感伤 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:08

    You forgot a couple of methods you need to override: getViewTypeCount() and getItemViewType(). These are not needed for lists where all rows are the same, but they are very important for your scenario. Implement these properly, and Android will maintain separate object pools for your headers and detail rows.

    Or, you could look at:

    • Jeff Sharkey's original SeparatedListAdapter (GPL)
    • My updated rendition (GPL)
    • My MergeAdapter, which can be used for this scenario as well (Apache)
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题