#一 想法產生 做Android開發的童鞋都知道,展示一個列表頁面需要以下幾個步驟: - 建立一個ListView或RecyclerView
- 建立Item佈局
- 建立Adapter
- 使用:將ListView與item關聯起來 按照以上做法,有多少列表,就有多少個Adapter。而這些adapter中有很多相似的方法,例如列表的初始化、新增、刪除、更新。。。這些步驟完全就是重複的,而且這樣做,很多業務邏輯要移到Adapter類中,顯得比較亂。因此便誕生了這樣一個想法。
#二 Adapter實現思路與事件(使用RecyclerView ) 資料: 資料可以使用泛型
private List<T1> list
複製程式碼
建構函式: Adapter需要繫結Item佈局,可以在建構函式中將佈局作為引數傳進去。新增@LayoutRes註解指明佈局資源。
public BaseAdapter(Context context, @LayoutRes int layout){
inflater=LayoutInflater.from(context);
this.context=context;
this.layout= layout;
list=new ArrayList<>();
}
複製程式碼
重寫父方法:onCreateViewHolder、getItemCount
複製程式碼
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(layout,parent,false);
return new BaseViewHolder(view);
}
@Override
public int getItemCount() {
return list.size();
}
複製程式碼
ViewHolder建立:生成ViewDataBinding並提供get方法(注意不要漏掉static)
public static class BaseViewHolder extends RecyclerView.ViewHolder {
private ViewDataBinding b;
public BaseViewHolder(View itemView) {
super(itemView);
b=DataBindingUtil.bind(itemView);
}
public ViewDataBinding getBinding() {
return b;
}
}
複製程式碼
回撥介面:用於ViewDataBinding的回撥,並在onBindViewHolder中將得到的holder與position回撥出去
public interface BindView<T2> {
void onBindViewHolder(T2 b,int position);
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
bindView.onBindViewHolder((T2) holder.getBinding(),position);
}
複製程式碼
使用:為adapter建立回撥。
adapter.setOnBindViewHolder(new BaseAdapter.BindView<ItemActionBinding>() {
@Override
public void onBindViewHolder(ItemActionBinding b, int position) {
b.setAction(actionInfos.get(position));
}
});
複製程式碼
#三 Adapter全部程式碼
package com.sunmoon.helper.adapter;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class BaseAdapter<T1, T2 extends ViewDataBinding> extends RecyclerView.Adapter<BaseAdapter.BaseViewHolder> {
private List<T1> list;
private LayoutInflater inflater;
private Context context;
@LayoutRes
private int layout;
private BindView<T2> bindView;
public BaseAdapter(Context context, @LayoutRes int layout) {
inflater = LayoutInflater.from(context);
this.context = context;
this.layout = layout;
list = new ArrayList<>();
}
public void initList(List<T1> list) {
this.list = list;
}
public void setOnBindViewHolder(BindView<T2> bindView) {
this.bindView = bindView;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(layout, parent, false);
return new BaseViewHolder(view);
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
bindView.onBindViewHolder((T2) holder.getBinding(), position);
}
@Override
public int getItemCount() {
return list.size();
}
public interface BindView<T2> {
void onBindViewHolder(T2 b, int position);
}
public static class BaseViewHolder extends RecyclerView.ViewHolder {
private ViewDataBinding b;
public BaseViewHolder(View itemView) {
super(itemView);
b = DataBindingUtil.bind(itemView);
}
public ViewDataBinding getBinding() {
return b;
}
}
}
複製程式碼