为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的效果
具体使用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的效果