public abstract class RecycleBaseAdapter<T, K extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
protected Context mContext;
protected List<T> dataList;
protected LayoutInflater mInflater;
protected RecycleViewAdapterListner<T> listner;
public RecycleBaseAdapter(Context mContext, List<T> dataList, RecycleViewAdapterListner<T> listner) {
this.mContext = mContext;
this.dataList = dataList;
this.listner = listner;
mInflater = LayoutInflater.from(mContext);
}
public RecycleBaseAdapter(Context mContext, List<T> dataList) {
this.mContext = mContext;
this.dataList = dataList;
this.listner = listner;
mInflater = LayoutInflater.from(mContext);
}
protected View inFlaterView(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
return mInflater.inflate(resource, root, attachToRoot);
}
@Override
public K onCreateViewHolder(ViewGroup parent, int viewType) {
return createViewholder(parent, viewType);
}
private T getItem(int position) {
return dataList.get(position);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
bindViewholder(getItem(position), (K) holder, position);
}
@Override
public int getItemCount() {
return dataList.size();
}
public abstract K createViewholder(ViewGroup parent, int viewType);
public abstract void bindViewholder(T t, K holder, int position);
}
public abstract class RecycleViewAdapterListner<T> implements RecycleViewAdapter<T>{
@Override
public void viewOnclick1(View view, int position, T t) {
}
@Override
public void viewOnclick2(View view, int position, T t) {
}
@Override
public void viewOnclick3(View view, int position, T t) {
}
@Override
public void viewOnclick4(View view, int position, T t) {
}
}
public interface RecycleViewAdapter<T> {
void viewOnclick1(View view, int position, T t);
void viewOnclick2(View view, int position, T t);
void viewOnclick3(View view, int position, T t);
void viewOnclick4(View view, int position, T t);
}
用法:
class MainIndexAdapter4 extends RecycleBaseAdapter<MainIndexBean.MainIndexInfo, MainIndexAdapter4.ViewHolder> {
public MainIndexAdapter4(Context mContext, List<MainIndexBean.MainIndexInfo> dataList) {
super(mContext, dataList);
}
@Override
public ViewHolder createViewholder(ViewGroup parent, int viewType) {
return new ViewHolder(inFlaterView(R.layout.report_time_recycle_adapter_layout4, parent, false));
}
@Override
public void bindViewholder(MainIndexBean.MainIndexInfo mainIndex, ViewHolder holder, int position) {
holder.ROER.setText(mainIndex.getROER());
}
class ViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.ROER)
TextView ROER;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}