Android: EfficientAdapter with two different Views

微笑、不失礼 提交于 2019-11-28 19:41:48

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:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!