public abstract class MyBaseExpandableAdapter<P, C> extends BaseExpandableListAdapter {
protected Context mContext;
protected List<P> mParentList;
protected List<List<C>> mChildList;
protected int mParentLayout;
protected int mChildLayout;
/**
* 参数过多,使用构造者模式?
*/
public MyBaseExpandableAdapter(Context mContext, List<P> mParentList, List<List<C>> mChildList, int parentLayout,
int childLayout) {
this.mContext = mContext;
this.mParentList = mParentList;
this.mChildList = mChildList;
this.mParentLayout = parentLayout;
this.mChildLayout = childLayout;
}
public abstract void convertParent(ViewHolder holder, P p, int groupPosition, boolean isExpanded);
public abstract void convertChild(ViewHolder holder, P p, C c, int groupPosition, int childPosition, boolean isLastChild);
@Override
public int getGroupCount() {
return mParentList == null ? 0 : mParentList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return (mChildList == null || mChildList.size() <= groupPosition || mChildList.get(groupPosition) == null) ?
0 : mChildList.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return mParentList == null ? null : mParentList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return (mChildList == null || mChildList.size() <= groupPosition || mChildList.get(groupPosition) == null) ?
null : mChildList.get(groupPosition).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) {
ViewHolder holder = ViewHolder.getInstance(mContext, convertView, mParentLayout);
convertParent(holder, mParentList.get(groupPosition), groupPosition, isExpanded);
return holder.getConvertView();
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder = ViewHolder.getInstance(mContext, convertView, mChildLayout);
convertChild(holder, mParentList.get(groupPosition), mChildList.get(groupPosition).get(childPosition), groupPosition, childPosition, isLastChild);
return holder.getConvertView();
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true; // 子列表可以点击
}
}
ViewHolder
/**
* 适配器通用的ViewHolder,而且 BaseAdapter 和 BaseExpandableListAdapter 都可以用
*/
public class ViewHolder {
private View convertView;
private SparseArray<View> views; // SparseArray效率比HashMap更高
private ViewHolder(View convertView) {
this.views = new SparseArray<>();
this.convertView = convertView;
convertView.setTag(this);
}
public static ViewHolder getInstance(Context context, View convertView, int layout) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(layout, null);
return new ViewHolder(convertView);
}
return (ViewHolder) convertView.getTag(); // 重用convertView的逻辑
}
public <T extends View> T findViewById(int id) {
View view = views.get(id);
if (view == null) {
view = convertView.findViewById(id);
views.append(id, view);
}
return (T) view;
}
public View getConvertView() {
return convertView;
}
}
使用方式:
public class TodaySignAdapter extends MyBaseExpandableAdapter<SignGroup, Record> {
public TodaySignAdapter(List<List<Record>> childLists, List<SignGroup> parentList, Context context) {
super(context, parentList, childLists,
R.layout.expandable_list_today_group, R.layout.expandable_list_today_child);
}
@Override
public void convertParent(ViewHolder holder, SignGroup signGroup, int groupPosition, boolean isExpanded) {
ImageView groupArrow = holder.findViewById(R.id.iv_group_arrow);
ImageView groupImage = holder.findViewById(R.id.iv_sign_group);
TextView signInfo = holder.findViewById(R.id.tv_sign_info);
TextView locationName = holder.findViewById(R.id.tv_sign_location_name);
signInfo.setText("打卡" + signGroup.signCount + "次/" + signGroup.duration);
locationName.setText(signGroup.locationName);
groupArrow.setImageResource(isExpanded ? R.mipmap.arrow_o : R.mipmap.arrow_n);
ImageLoader.getInstance().displayImage(URLs.HEAD_URL + signGroup.fileName, groupImage);
}
@Override
public void convertChild(ViewHolder holder, SignGroup signGroup, Record record, int groupPosition, int childPosition, boolean isLastChild) {
ImageView childImage = holder.findViewById(R.id.iv_sign_file_name);
TextView dateSign = holder.findViewById(R.id.tv_sign_date);
ImageLoader.getInstance().displayImage(URLs.HEAD_URL + record.getFileName(), childImage);
dateSign.setText(TimeTransferUtil.getTime(Long.parseLong(record.getDateSign())));
}
}