ExpandbleListView封裝Adapter

爲ExpandableListView打造通用的BaseAdapter,只需幾行代碼,不用繼承BaseExpandableListview並重寫那些方法。
特點,簡單,複用view,同時支持ExpandableListview和ListView。

源碼已上傳至Github,地址爲:

https://github.com/crook3/ExpandableListviewAdapter

使用方法爲:
compile ‘com.tranwon:expandablelistview:1.0.1’

話說安卓開發裏面最常用的控件之一ListView,每次使用都要自己實現BaseAdapter,重新裏面的各種方法。本來一個ListView實現一個BaseAdapter也還好,但是實際情況往往是一個app裏面需要多個ListView,實現多BaseAdapter,更重要的是代碼重複過多。這對於我們偉大的程序員來說是不能容忍的,於是就有了各種各樣的對ListView的BaseAdapter進行封裝,打造通用Adapter的第三方工具。
比如這位https://github.com/hongyangAndroid/baseAdapter
和這位https://github.com/crook3/ExpandableListviewAdapter

基於同樣的想法,如果app裏面需要用到ExpandableListView這種二級列表,我們大可以自己去實現BaseExpandableListViewAdapter,然後重寫裏面多個方法,以及一遍又一遍…。直到有一天,自己都煩了,是滴,我煩了。所以我也照貓畫虎,自己擼了一個通用的BaseAdapter。自從有了它,再也不用寫Adapter裏面的一大堆方法了,只需一個new搞定。就是這麼簡單!不僅如此,工具同時支持ExpandableListView和ListView,原因是讓優秀的你只需引用一個庫即可。

簡單的原理介紹
1.封裝ViewHolde

public class MyViewHolderExpandbleListView {
    private SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;

    public MyViewHolderExpandbleListView(Context context,
                                         ViewGroup parent, int layoutId, int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        mConvertView.setTag(this);
    }

    public static MyViewHolderExpandbleListView get(Context context, View convertView,
                                                    ViewGroup parent, int layoutId, int position) {
        if (convertView == null) {
            return new MyViewHolderExpandbleListView(context, parent, layoutId, position);
        } else {
            MyViewHolderExpandbleListView holder = (MyViewHolderExpandbleListView) convertView.getTag();
            holder.mPosition = position;
            return holder;
        }
    }
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }
    public View getConvertView() {
        return mConvertView;
    }
}

2.繼承BaseExpandableListAdapter後封裝

protected Context mContext;
protected List<T> mDatasParent;
protected List<T> mDatasChild;
protected LayoutInflater mInflater;
private int mLayoutIdParent;
private int mLayoutIdChild;
public MyBaseAdapterExpandbleListview(Context context, List<T> datasParent,  List<T> datasChild, int layoutIdParent,int layoutIdChild) {
    this.mContext = context;
    mInflater = LayoutInflater.from(context);
    this.mDatasParent = datasParent;
    this.mDatasChild = datasChild;
    this.mLayoutIdParent = layoutIdParent;
    this.mLayoutIdChild = layoutIdChild;
}
@Override
public int getGroupCount() {
    return mDatasParent.size();
}
@Override
public int getChildrenCount(int i) {
    return 1;
}
@Override
public T getGroup(int i) {
    return mDatasParent.get(i);
}
@Override
public T getChild(int i, int i1) {
    return mDatasChild.get(i);
}
@Override
public long getGroupId(int i) {
    return i;
}
@Override
public long getChildId(int i, int i1) {
    return i;
}
@Override
public boolean hasStableIds() {
    return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
    MyViewHolderExpandbleListView holder = MyViewHolderExpandbleListView.get(mContext,view,viewGroup,mLayoutIdParent,i);
    convertParent(holder, getGroup(i));
    return holder.getConvertView();
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
    MyViewHolderExpandbleListView holder = MyViewHolderExpandbleListView.get(mContext,view,viewGroup,mLayoutIdChild,i);
    convertChild(holder, getChild(i,i1));
    return holder.getConvertView();
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return false;
}
public abstract void convertParent(MyViewHolderExpandbleListView holder, T t);
public abstract void convertChild(MyViewHolderExpandbleListView holder, T t);

3.使用
引入
compile ‘com.tranwon:expandablelistview:1.0.1’

只需要簡單的將Adapter繼承CommonAdapter,複寫convert方法即可。省去了自己編寫ViewHolder等大量的重複的代碼。
可以通過holder.getView(id)拿到任何控件。

具體使用ExpandableListView的代碼如下:


MyBaseAdapterExpandbleListview<String> adapter=new MyBaseAdapterExpandbleListview<String>(
            getApplicationContext(),mListParentData,mListChildData,R.layout.item_parent
            ,R.layout.item_child) {
        @Override
        public void convertParent(MyViewHolderExpandbleListView holder, String s, int parentPosition) {
            TextView tv= holder.getView(R.id.tv1);
            tv.setText(s);
        }
        @Override
        public void convertChild(MyViewHolderExpandbleListView holder, String s, int parentPosition, int childPosition) {
            TextView tv= holder.getView(R.id.tv1);
            tv.setText(s);
        }
    };

ExpandableListView的效果

image

具體使用ListView的代碼如下:


MyBaseAdapterListview<String> adapter=new MyBaseAdapterListview<String>(
            getApplicationContext(),mListData,R.layout.listview_item) {
        @Override
        public void convert(MyViewHolderExpandbleListView holder, String s) {
            TextView tv= holder.getView(R.id.tv1);
            tv.setText(s);
            ImageView im=holder.getView(R.id.image);
            im.setImageResource(R.mipmap.ic_launcher);
        }
    };

ListView的效果

image2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章