前言
RecycleView每次使用都要创建 Adapter 的类文件,而Adapter每次都基本差不错,因此可以对RecycleAdapter进行封装,这样就不用每次创建Adapter的类文件,提升开发效率。
使用封装好的RecycleAdapter例子:
BaseRecycleAdapter<String, MyViewHolder> recycleAdapter = new BaseRecycleAdapter<>(generatorData(20), R.layout.item_recycle);
recycleAdapter
.setFactory(new IViewHolderFactory<MyViewHolder>() {
@Override
public MyViewHolder generatorViewHolder(View view, BaseViewHolder.OnItemClickListener onItemClickListener) {
return new MyViewHolder(view, onItemClickListener);
}
})
.setOnItemClickListener(new BaseViewHolder.OnItemClickListener() {
@Override
public void onItemClicked(View v, int position) {
Toast.makeText(MainActivity.this,String.valueOf(position),Toast.LENGTH_LONG).show();
}
});
mMainBing.rvContent.setAdapter(recycleAdapter);
class MyViewHolder extends BaseViewHolder<String> {
ItemRecycleBinding item;
public MyViewHolder(View itemView, OnItemClickListener onItemClickedListener) {
super(itemView, onItemClickedListener);
item = DataBindingUtil.bind(itemView);
}
@Override
public void setData(String s) {
item.setPosition(s);
}
}
这样对RecycleView的数据填充便完成了,整个过程中没有创建新的Adapter类文件,如果使用Lambda 代码还可以进一步简化。
Adapter基本封装
ReccycleAdapter
public class BaseRecycleAdapter<D,V extends BaseViewHolder<D>> extends RecyclerView.Adapter<V> {
private List<D> mDatas;
private IViewHolderFactory<V> mFactory;
private int mLayoutId;
private BaseViewHolder.OnItemClickListener mOnItemClickedListener;
public BaseRecycleAdapter(List<D> datas,int layoutId){
this.mDatas=datas;
this.mLayoutId=layoutId;
}
@Override
public V onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(mLayoutId,parent,false);
return mFactory.generatorViewHolder(view,mOnItemClickedListener);
}
@Override
public void onBindViewHolder(V holder, int position) {
holder.setData(mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas==null?0:mDatas.size();
}
public BaseRecycleAdapter setOnItemClickListener(BaseViewHolder.OnItemClickListener onItemClickListener){
this.mOnItemClickedListener =onItemClickListener;
return this;
}
public BaseRecycleAdapter setFactory(IViewHolderFactory<V> factory){
this.mFactory=factory;
return this;
}
public void updateData(final List<D> newDatas){
DiffUtil.DiffResult result=DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return mDatas.size();
}
@Override
public int getNewListSize() {
return newDatas.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return mDatas.get(oldItemPosition)==newDatas.get(newItemPosition);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return mDatas.get(oldItemPosition).equals(newDatas.get(newItemPosition));
}
},true);
result.dispatchUpdatesTo(this);
}
}
ViewHolder
public abstract class BaseViewHolder<D> extends RecyclerView.ViewHolder {
public BaseViewHolder(View itemView, final OnItemClickListener onItemClickedListener){
super(itemView);
if(onItemClickedListener!=null){
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickedListener.onItemClicked(v,getAdapterPosition());
}
});
}
}
public abstract void setData(D d);
public interface OnItemClickListener{
void onItemClicked(View v,int position);
}
ViewHolderFactory
public interface IViewHolderFactory<T> {
T generatorViewHolder(View view, BaseViewHolder.OnItemClickListener onItemClickListener);
}
添加Header(添加footer与此相同)
public class HeaderAdapter extends RecyclerView.Adapter{
private RecyclerView.Adapter mAdapter;
private List<View> mHeaders;
public HeaderAdapter(List<View> headers){
this.mHeaders=headers;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType<mHeaders.size()){
return new CommonViewHolder(mHeaders.get(viewType));
}
else {
return mAdapter.onCreateViewHolder(parent, viewType-mHeaders.size());
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
mAdapter.onBindViewHolder(holder,position);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List payloads) {
mAdapter.onBindViewHolder(holder, position, payloads);
}
@Override
public int getItemViewType(int position) {
if(position>=mHeaders.size()){
return mHeaders.size()+mAdapter.getItemViewType(position);
}else{
return position;
}
}
........
}
代码已上传到GitHub:https://github.com/guhongya/base-library