我们使用Data Binding库一步步为RecyclerView打造通用的Adapter,今天我们先来编写单一Item时的Adapter:SimpleAdapter。至于如何在项目中添加Data Binding库,在次不再涉及,敬请自行Google或Android DataBinding介绍。
第一步:通用的ViewHolder
package com.dfsebook.myrecycleradapter.view; import android.databinding.ViewDataBinding; import android.support.v7.widget.RecyclerView; import android.view.View; /** * Created by Administrator on 16-7-18. */ public class ViewHolder extends RecyclerView.ViewHolder{ private ViewDataBinding binding; public ViewDataBinding getBinding() { return binding; } public void setBinding(ViewDataBinding binding) { this.binding = binding; } public ViewHolder(View itemView) { super(itemView); } }
第二步:通用Adapter
package com.dfsebook.myrecycleradapter.adapter; import android.databinding.DataBindingUtil; import android.databinding.ViewDataBinding; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.ViewGroup; import com.dfsebook.myrecycleradapter.view.ViewHolder; import java.util.List; /** * Created by Administrator on 16-7-18. */ public class SimpleAdapter<T> extends RecyclerView.Adapter<ViewHolder>{ private List<T> mDatas; private int layoutId; private int brId; public SimpleAdapter(List<T> mDatas, int layoutId, int brId) { this.mDatas = mDatas; this.layoutId = layoutId; this.brId = brId; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInf
通过Data Binding为RecyclerView打造通用Adapter
最新推荐文章于 2024-06-25 11:35:11 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)