Java中SeparatedListAdapter类的实现
生活随笔
收集整理的這篇文章主要介紹了
Java中SeparatedListAdapter类的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需要引入的包有:
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.Adapter; import android.widget.ArrayAdapter; import android.widget.BaseAdapter;import java.util.LinkedHashMap; import java.util.Map;以下開始類的構建: public class SeparatedListAdapter extends BaseAdapter {public final Map<String, Adapter> sections = new LinkedHashMap<>();public final ArrayAdapter<String> headers;public final static int TYPE_SECTION_HEADER = 0;首先,聲明一個終態的Map<String, Adapter>型變量sections,鍵值對的類型實參為String和Adapter,且創建一個新的LinkedHashMap()型的變量,賦給變量sections。和一個ArrayAdapter型的變量headers,且類型實參為String。LinkedHashMap 是 Map 的實現類,Map是一個公有接口,LinkedHashMap等一系列Map是公有類,繼承于HashMap,調用Map接口。 public SeparatedListAdapter(Context context) {headers = new ArrayAdapter<String>(context, R.layout.list_header);}public void addSection(String section, Adapter adapter) {this.headers.add(section);this.sections.put(section, adapter);}接著,將addSection方法中傳入的參數分別寫入headers和sections中。
其中,headers類型為ArrayAdapter,sections類型為LinkedHashMap,結構分別為:ArrayAdapter(Context, int)和LinkedHashMap(int, float), add(sections)表示將section插入隊列headers隊尾,put(section, adapter)表示將字符串section置于adapter鍵位。
public Object getItem(int position) {for (Object section : this.sections.keySet()) {Adapter adapter = sections.get(section);int size = adapter.getCount() + 1;// check if position inside this sectionif (position == 0)return section;if (position < size)return adapter.getItem(position - 1);// otherwise jump into next sectionposition -= size;}return null;}獲取Item方法,在sections.keySet()中遍歷keys,(其中keySet()返回全部的keys)將adapter的數量賦給size,如果Item的位置參數為0,則返回第一個,否則,倒敘遍歷查找
public int getCount() {// total together all sections, plus one for each section headerint total = 0;for (Adapter adapter : this.sections.values())total += adapter.getCount() + 1;return total;}遞歸得到sections數量 public int getViewTypeCount() {// assume that headers count as one, then total all sectionsint total = 1;for (Adapter adapter : this.sections.values())total += adapter.getViewTypeCount();return total;}返回adapter的types of view 的數量 public int getItemViewType(int position) {int type = 1;for (Object section : this.sections.keySet()) {Adapter adapter = sections.get(section);int size = adapter.getCount() + 1;// check if position inside this sectionif (position == 0)return TYPE_SECTION_HEADER;if (position < size)return type + adapter.getItemViewType(position - 1);// otherwise jump into next sectionposition -= size;type += adapter.getViewTypeCount();}return -1;}遞歸得到position位置的viewtype public boolean isEnabled(int position) {return (getItemViewType(position) != TYPE_SECTION_HEADER);}public View getView(int position, View convertView, ViewGroup parent) {int sectionnum = 0;for (Object section : this.sections.keySet()) {Adapter adapter = sections.get(section);int size = adapter.getCount() + 1;// check if position inside this sectionif (position == 0)return headers.getView(sectionnum, convertView, parent);if (position < size)return adapter.getView(position - 1, convertView, parent);// otherwise jump into next sectionposition -= size;sectionnum++;}return null;}public long getItemId(int position) {return position;}
總結
以上是生活随笔為你收集整理的Java中SeparatedListAdapter类的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 缺少com.umeng.analytic
- 下一篇: Android项目中Bluetooth类