android万能适配器之SuperAdapter

对于传统的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 extends BaseAdapter {

    protected Context mContext;
    protected int mLayoutResId;
    protected List mList;
    protected IMultiItemViewType mMultiItemViewType;

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

    public BaseSuperAdapter(Context context, List data, IMultiItemViewType multiItemViewType) {
        this.mContext = context;
        this.mList = data == null ? new ArrayList() : 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 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 items) {
        mList.clear();
        addAll(items);
    }

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

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

    public List getAllData() {
        return mList;
    }

}


源码就这些了。具体用法:

(1)listView中的SingleItem(单布局)
     
	初始化:
	
       new ListSingleAdapter(getContext(), names, R.layout.item_type1)
 
  
 
  
(2)listView中的MultiItem(多布局)
android万能适配器之SuperAdapter_第1张图片
	
	初始化:
其他的像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

你可能感兴趣的:(android,开发)