对于迭代周期短的项目来说,需要也页面的灵活性和可扩展性。列表是使用很广泛的组件。现在针对ListView 来进行扩展,方便协作开发和扩展。以后还将陆续记录recycleview和scrollview的扩展。
1.目标。ListView支持添加多个适配器
如:mListView.addAdapter(adapterA);mListView.addAdapter(adapterB)。其中adapterA是列表前几栏的适配器,adapterB是列表后面几栏的适配器。
2.实现
public static SectionListViewAdapter extends BaseAdapter{ public final Mapsections = new LinkedHashMap(); public void addAdapter(String sectionName, Adapter adapter){ sections.put(sectionName, adapter); } public Object getItem(int position){ for(Object sectionName : sections.keySet()){ Adapter adapter = sections.get(sectionName); int size = adapter.getCount(); //检查position是否在此adapter内 if(position < size) return adapter.getItem(position); position -= size; } return null; } public int getCount(){ int total = 0; for(Adapter adapter : sections.values()) total += adapter.getCount; return total; } public int getViewTypeCount() { int total = 0; for(Adapter adapter :sections.values()) total += adapter.getViewTypeCount(); return total; } public View getView(int position, View convertiew, ViewGroup parent){ for(Object sectionName : sections,keySet()){ Adapter adapter = sections.get(sectionName); int size = adapter.getcount(); if(position < size) return adapter.getView(position); position -= size; } return null; } public long getItemId(int position){ return position; } }
网友评论