适配器父类
/**
*版权:
*@创建者:shuquan
*创建时间:2020/9/23
*模块:
*描述:适配器父类
*/
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
List<T> mData;//集合对象
int mLayoutResId;//布局LayoutID
Context context;//上下文
public BaseAdapter(@LayoutRes int layoutResId, @Nullable List<T> data, Context context) {
this.mData = data == null ? new ArrayList<T>() : data;
this.context = context;
if (layoutResId != 0) {
this.mLayoutResId = layoutResId;
}
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(mLayoutResId, parent, false);
return new BaseViewHolder(itemView);
}
@Override
public void onBindViewHolder(final BaseViewHolder holder, int position) {
bindViewHolder(holder, mData.get(position), position);
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return mData.size();
}
public abstract void bindViewHolder(BaseViewHolder holder, T itemData, int position);
public Context getContext() {
return context;
}
/**
* @param:
*创建时间:2020/10/23
*描述:获取集合对象
*/
public List<T> getDataList() {
return mData;
}
/**
* @param: dataList 数据源
*创建时间:2020/10/23
*描述:设置数据
*/
public void setDataList(List<T> dataList) {
this.mData = dataList;
notifyDataSetChanged();
}
}
子类使用
/**
* 版权:
* 创建者:shuquan
* 创建时间:2020/9/23
* 描述:主页菜单适配器
*/
public class ItmeAdapter extends BaseAdapter<ItmeData>{
ItmeApadterCallback callback;//点击接口回调
public ItmeAdapter(Context context, List<ItmeData> dataList, ItmeApadterCallback callback) {
super(R.layout.home_itme_layout, dataList, context);//设置布局和对象
this.callback = callback;
}
@Override
public void bindViewHolder(BaseViewHolder holder,final ItmeData itemData, int position) {
holder.setText(R.id.tv_tiem,getContext().getResources().getString(itemData.tiem));
holder.setVisible(R.id.tvp_number,itemData.number==0?false:true);
}
}
BaseViewHolder(主要类)
/**
*版权:
*@创建者:shuquan
*创建时间:2020/10/23
*模块
*描述:BaseViewHolder
*/
public class BaseViewHolder extends RecyclerView.ViewHolder {
private final SparseArray<View> views;
public View convertView;
public BaseViewHolder(final View view) {
super(view);
this.views = new SparseArray<>();
convertView = view;
}
/**
* 返回传染ID的View
* @param viewId
* @return
*/
public View getViews(@IdRes int viewId) {
View view = getView(viewId);
return view;
}
public void setText(@IdRes int viewId, CharSequence value) {
TextView view = getView(viewId);
view.setText(value);
}
public void setImageResource(@IdRes int viewId, @DrawableRes int imageResId) {
ImageView view = getView(viewId);
view.setImageResource(imageResId);
}
public void setVisible(@IdRes int viewId, boolean visible) {
View view = getView(viewId);
view.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
}
@Deprecated
public void setOnClickListener(@IdRes int viewId, View.OnClickListener listener) {
View view = getView(viewId);
view.setOnClickListener(listener);
}
@Deprecated
public void setOnTouchListener(@IdRes int viewId, View.OnTouchListener listener) {
View view = getView(viewId);
view.setOnTouchListener(listener);
}
public <T extends View> T getView(@IdRes int viewId) {
View view = views.get(viewId);
if (view == null) {
view = itemView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
}
可以根据自己的实际需求进行封装