Databinding 的使用

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

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值