android萬能介面卡之SuperAdapter

pszh發表於2016-04-28

對於傳統的Adapter的寫法,相信大家都是很熟悉的,然而在一個大型的專案中,我們肯定不會每次都去繼承BaseAdapter,這樣會很大的增加我們的勞動量,這裡給大家介紹一個比較牛的SuperAdapter


首先來看下SuperAdapter的原始碼:

前兩個是構造方法:沒什麼說的,
onCreate()方法去建立服用的ViewHolder



接下來看下BaseViewHolder.get()的方法:和傳統的寫法很類似了,

到這裡了,我們索性看下BaseViewHolder的其他方法:在這裡封裝了類似

 public BaseViewHolder setText(int viewId, CharSequence value) {
        TextView view = getView(viewId);
        if (TextUtils.isEmpty(value))
            view.setText("");
        else
            view.setText(value);
        return this;
    }

    public BaseViewHolder setImageResource(int viewId, int imageResId) {
        ImageView view = getView(viewId);
        view.setImageResource(imageResId);
        return this;
    }
的很多的程式碼,用來傳入item中的子view的id,和顯示的資源來進行繫結

最後看下BaseSuperAdaper這個類 (它繼承自底層的BaseAdapter實現的程式碼也是很簡單的,平時我們都有用到)

public abstract class BaseSuperAdapter<T, H extends BaseViewHolder> extends BaseAdapter {

    protected Context mContext;
    protected int mLayoutResId;
    protected List<T> mList;
    protected IMultiItemViewType<T> mMultiItemViewType;

    public BaseSuperAdapter(Context context, List<T> data, int layoutResId) {
        this.mContext = context;
        this.mList = data == null ? new ArrayList<T>() : new ArrayList<>(data);
        this.mLayoutResId = layoutResId;
    }

    public BaseSuperAdapter(Context context, List<T> data, IMultiItemViewType<T> multiItemViewType) {
        this.mContext = context;
        this.mList = data == null ? new ArrayList<T>() : new ArrayList<>(data);
        this.mMultiItemViewType = multiItemViewType;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public T getItem(int position) {
        if (position >= mList.size())
            return null;
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        if (mMultiItemViewType != null)
            return mMultiItemViewType.getViewTypeCount();
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (mMultiItemViewType != null) {
            return mMultiItemViewType.getItemViewType(position, mList.get(position));
        }
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final H viewHolder = onCreate(getItemViewType(position), convertView, parent);
        T item = getItem(position);
        onBind(viewHolder, position, item);
        return viewHolder.getItemView();
    }

    protected abstract H onCreate(int viewType, View convertView, ViewGroup parent);

    /**
     * Abstract method for binding view and data.
     *
     * @param holder   ViewHolder
     * @param position position
     * @param item     data
     */
    protected abstract void onBind(H holder, int position, T item);

    public void add(T item) {
        mList.add(item);
        notifyDataSetChanged();
    }

    public void add(T item, boolean isChanged) {
        mList.add(item);
        if (isChanged)
            notifyDataSetChanged();
    }

    public void add(int index, T item) {
        mList.add(index, item);
        notifyDataSetChanged();
    }

    public void addAll(List<T> items) {
        mList.addAll(items);
        notifyDataSetChanged();
    }

    public void remove(T item) {
        mList.remove(item);
        notifyDataSetChanged();
    }

    public void remove(int index) {
        mList.remove(index);
        notifyDataSetChanged();
    }

    public void remove(int index, boolean isChange) {
        mList.remove(index);
        if (isChange)
            notifyDataSetChanged();
    }

    public void set(T oldItem, T newItem) {
        set(mList.indexOf(oldItem), newItem);
    }

    public void set(int index, T item) {
        mList.set(index, item);
        notifyDataSetChanged();
    }

    public void replaceAll(List<T> items) {
        mList.clear();
        addAll(items);
    }

    public boolean contains(T item) {
        return mList.contains(item);
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public List<T> getAllData() {
        return mList;
    }

}


原始碼就這些了。具體用法:

(1)listView中的SingleItem(單佈局)
     
	初始化:
	
       new ListSingleAdapter(getContext(), names, R.layout.item_type1)

(2)listView中的MultiItem(多佈局)
	
	初始化:
其他的像GridView.RecyclerView的Adaper用法都是一樣的
最後附上demo的下載地址:http://www.oschina.net/code/snippet_2702417_55790
eclispe原始碼:http://download.csdn.net/detail/u013424496/9506142

最新版的SuperAdapter:https://github.com/byteam/SuperAdapter

相關文章