封装RecyclerView.Adapter
/**
* 处理一些通用的步骤,实现Item 点击事件
* T 表示ViewHolder E 表示实体模型
*/
public abstract class BaseRecyclerAdapter<T extends RecyclerView.ViewHolder, E> extends RecyclerView.Adapter<T> implements View.OnClickListener {
private List<E> datas;
private ItemClickListener itemClickListener;
public BaseRecyclerAdapter(List<E> datas) {
this.datas = datas;
}
protected abstract int getItemContentId(int viewType);
protected abstract T getViewHolder(View view, int viewType);
/*
* viewType 表示item 布局的种类,重载getItemViewType 方法决定
* */
@Override
public T onCreateViewHolder(ViewGroup parent, int viewType) {
// Log.i("TAG", "onCreateViewHolder: parent");
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(getItemContentId(viewType), null);
return getViewHolder(itemView, viewType);
}
@Override
public void onBindViewHolder(T holder, int position) {
holder.itemView.setTag(position);
holder.itemView.setOnClickListener(this);
}
@Override
public int getItemCount() {
return datas.size();
}
@Override
public long getItemId(int position) {
return position;
}
/*
* 获取指定位置的数据
* */
public E getItem(int position) {
return datas.get(position);
}
/*
* 在第一个位置添加一个数据
* */
public void addItemInFirst(E itemData) {
this.datas.add(0, itemData);
notifyDataSetChanged();
}
/**
* 获取数据集合
* @return
*/
public List<E> getDatas() {
return this.datas;
}
/**
* 删除一个元素
* @param position 位置
*/
public void removeItem(int position) {
this.datas.remove(position);
notifyDataSetChanged();
}
/*
* 追加一个数据
* */
public void addItem(E itemData) {
this.datas.add(itemData);
notifyDataSetChanged();
}
/*
* 追加一个数据集合
* */
public void addItems(List<E> newDatas) {
this.datas.addAll(newDatas);
notifyDataSetChanged();
}
/*
* 替换现有数据集合
* */
public void updateItems(List<E> newDatas) {
this.datas = newDatas;
notifyDataSetChanged();
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
@Override
public void onClick(View view) {
if(itemClickListener != null) {
itemClickListener.onItemClick(view, (Integer) view.getTag());
}
}
// public List<E> getDatas() {
// return datas;
// }
// public E getItem(int position) {
// return datas.get(position);
// }
// public E removeItem(int position) {
// E item = datas.remove(position);
// notifyDataSetChanged();
// return item;
// }
// public void resetData(List<E> datas) {
// this.datas = datas;
// notifyDataSetChanged();
// }
//
// public void clearData() {
// this.datas.clear();
// notifyDataSetChanged();
// }
//
// public void addToData(List<E> datas) {
// this.datas.addAll(datas);
// notifyDataSetChanged();
// }
}
自定义RecyclerViewDivider
public class RecyclerViewDivider extends RecyclerView.ItemDecoration {
// private Context mContext;
protected Drawable mDivider;
private int mOrientation;
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
public static final int[] ATRRS = new int[]{android.R.attr.listDivider};
/*
* 构造方法
* 采用默认的divider: android.R.attr.listDivider
* */
public RecyclerViewDivider(Context mContext, int mOrientation) {
if(mOrientation != HORIZONTAL_LIST && mOrientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
this.mOrientation = mOrientation;
// this.mContext = mContext;
TypedArray a = mContext.obtainStyledAttributes(ATRRS);
this.mDivider = a.getDrawable(0);
a.recycle();
}
/*
* 构造方法
* 传divider id
* */
public RecyclerViewDivider(Context mContext, int mOrientation, int dividerResId) {
if(mOrientation != HORIZONTAL_LIST && mOrientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
this.mOrientation = mOrientation;
// this.mContext = mContext;
this.mDivider = ContextCompat.getDrawable(mContext, dividerResId);
}
/*
* 构造方法
* 直接传divider
* */
public RecyclerViewDivider(Context mContext, int mOrientation, Drawable divider) {
if(mOrientation != HORIZONTAL_LIST && mOrientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
this.mOrientation = mOrientation;
// this.mContext = mContext;
this.mDivider = divider;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if(mOrientation == HORIZONTAL_LIST) {
//画竖线,就是往右偏移一个分割线的宽度
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}else {
//画横线,就是往下偏移一个分割线的高度
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
}
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == HORIZONTAL_LIST){
drawVerticalLine(c, parent, state);
}else {
drawHorizontalLine(c, parent, state);
}
}
//画横线
public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state){
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++){
final View child = parent.getChildAt(i);
//获得child的布局信息
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
//画竖线
public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state){
int top = parent.getPaddingTop();
int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++){
final View child = parent.getChildAt(i);
//获得child的布局信息
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
封装二级列表思路
关键是根据数据结构,重写getViewType 方法,从而区分开子item