1、采用Databinding 的方式可以省去之前我们代码中的findViewbyId 的写法:
2、在使用的时候首先得在build.gradle 文件中添加:
android {
dataBinding {
enabled = true
}
}
3、在xml文件中的根标签需要使用layout
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="product"
type="com.example.jetpackdemo.bean.ProductEntity" />
</data>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:minHeight="100dp"
android:orientation="horizontal"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/lblName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{product.name}" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</layout>
使用的时候可以采用 android:text="@{product.name}",前提得有对应的实体类,相关实体类请查看下面的链接:
https://blog.csdn.net/nmsoftklb/article/details/89840241
4、在Adapter 中的使用:
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.HomeViewHolder> {
private List<ProductEntity> mProductList;
public HomeAdapter(){}
public void setProductList(final List<ProductEntity> productList){
if (mProductList == null){
mProductList = productList;
//notifyItemRangeInserted(0,productList.size());
}else {
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return mProductList.size();
}
@Override
public int getNewListSize() {
return productList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return mProductList.get(oldItemPosition).getId() == productList.get(newItemPosition).getId();
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
ProductEntity newProduct = productList.get(newItemPosition);
ProductEntity oldProduct = mProductList.get(oldItemPosition);
return newProduct.getId() == oldProduct.getId() && newProduct.getName().equals(oldProduct.getName());
}
});
mProductList = productList;
result.dispatchUpdatesTo(this);
}
}
@NonNull
@Override
public HomeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
HomeItemBinding binging = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.home_item,parent,false);
return new HomeViewHolder(binging);
}
@Override
public void onBindViewHolder(@NonNull HomeViewHolder holder, int position) {
holder.binging.setProduct(mProductList.get(position));
}
@Override
public int getItemCount() {
return mProductList == null ? 0: mProductList.size() ;
}
@Override
public long getItemId(int position) {
return mProductList.get(position).getId();
}
static class HomeViewHolder extends RecyclerView.ViewHolder {
private HomeItemBinding binging;
public HomeViewHolder(HomeItemBinding binding){
super(binding.getRoot());
this.binging = binding;
}
}
}
注:程序中 holder.binging.setProduct(mProductList.get(position)),setProduct这个方法是来自于xml文件中<variable>标签中的name属性。
程序中的
HomeItemBinding binging = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.home_item,parent,false);
HomeItemBinding 这个类会根据相应的layout 文件名自动生成,生成的规则就是layout 的名称+Binding