作者:陈玲琳2013 | 来源:互联网 | 2024-12-07 10:41
本文介绍了一种针对ExpandableListView的通用BaseAdapter封装方法,通过这种方式,开发者可以在不直接继承BaseExpandableListView或重写其方法的情况下,轻松创建适配器。这种方法不仅简化了代码,还提高了组件的复用性,支持ExpandableListView和普通的ListView。
项目源码已托管在GitHub上,链接如下:
https://github.com/crook3/ExpandableListviewAdapter
使用方法:
在项目的build.gradle文件中添加依赖:
implementation 'com.tranwon:expandablelistview:1.0.1'
在Android应用开发中,ListView是使用频率较高的控件之一。通常情况下,为了实现ListView的功能,开发者需要自定义BaseAdapter,并重写其中的方法。当一个应用中包含多个ListView时,这种重复的工作量会显著增加,导致代码冗余。为此,许多开发者选择封装通用的BaseAdapter,以减少重复劳动。
例如:https://github.com/hongyangAndroid/baseAdapter
基于类似的需求,当应用中需要使用到ExpandableListView(一种支持二级列表显示的控件)时,我们可以自定义一个BaseExpandableListViewAdapter,避免多次重复编写相同的方法。这个封装后的适配器,可以让开发者只需几行代码就能完成配置,极大地简化了工作流程。
原理简介
1. 封装ViewHolder
public class MyViewHolderExpandbleListView {
private SparseArray mViews;
private int mPosition;
private View mConvertView;
public MyViewHolderExpandbleListView(Context context, ViewGroup parent, int layoutId, int position) {
this.mPosition = position;
this.mViews = new SparseArray<>();
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 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 mDatasParent;
protected List mDatasChild;
protected LayoutInflater mInflater;
private int mLayoutIdParent;
private int mLayoutIdChild;
public MyBaseAdapterExpandbleListview(Context context, List datasParent, List 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 groupPosition) {
return 1;
}
@Override
public T getGroup(int groupPosition) {
return mDatasParent.get(groupPosition);
}
@Override
public T getChild(int groupPosition, int childPosition) {
return mDatasChild.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
MyViewHolderExpandbleListView holder = MyViewHolderExpandbleListView.get(mContext, convertView, parent, mLayoutIdParent, groupPosition);
convertParent(holder, getGroup(groupPosition));
return holder.getConvertView();
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
MyViewHolderExpandbleListView holder = MyViewHolderExpandbleListView.get(mContext, convertView, parent, mLayoutIdChild, childPosition);
convertChild(holder, getChild(groupPosition, childPosition));
return holder.getConvertView();
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public abstract void convertParent(MyViewHolderExpandbleListView holder, T item);
public abstract void convertChild(MyViewHolderExpandbleListView holder, T item);
3. 使用示例
通过继承CommonAdapter并覆写convert方法,可以非常方便地使用这个适配器。例如,对于ExpandableListView的使用,可以参考以下代码:
MyBaseAdapterExpandbleListview adapter = new MyBaseAdapterExpandbleListview<>(
getApplicationContext(),
mListParentData,
mListChildData,
R.layout.item_parent,
R.layout.item_child) {
@Override
public void convertParent(MyViewHolderExpandbleListView holder, String item, int parentPosition) {
TextView tv = holder.getView(R.id.tv1);
tv.setText(item);
}
@Override
public void convertChild(MyViewHolderExpandbleListView holder, String item, int parentPosition, int childPosition) {
TextView tv = holder.getView(R.id.tv1);
tv.setText(item);
}
};
对于ListView的使用,可以参考以下代码:
MyBaseAdapterListview adapter = new MyBaseAdapterListview<>(
getApplicationContext(),
mListData,
R.layout.listview_item) {
@Override
public void convert(MyViewHolderExpandbleListView holder, String item) {
TextView tv = holder.getView(R.id.tv1);
tv.setText(item);
ImageView im = holder.getView(R.id.image);
im.setImageResource(R.mipmap.ic_launcher);
}
};