RecyclerView使用Data Binding进行数据填充

关于Data Binding的优点我在这里就不啰嗦了,在这里给大家介绍一个例子,利用Data Binding为RecyclerView填充数据。

首先需要做的工作是配置Data Binding的设置,在build.gradle文件中添加

    dataBinding {
        enabled = true
    }

因为我使用了RecyclerView和CardView,所以也要引用相应的包,也是在build.gradle中添加

    compile 'com.android.support:recyclerview-v7:25.+'
    compile 'com.android.support:cardview-v7:25.+'

接下来是编写布局文件,它与我们之前写的布局文件只有些许差异

listview_userlist.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/user_list"
            android:textSize="@dimen/button_text_size"
            android:textColor="@color/black"
            android:gravity="center"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/lv_userlist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>
</layout>

user_item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="ly.recyclerviewtest.model.User"/>
        <variable name="user" type="User"/>
    </data>
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:padding="5dp" >

            <TextView
                android:clickable="true"
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginBottom="10dp"
                android:layout_marginRight="10dp"
                android:text="@{user.name ?? @string/app_name}"
                android:gravity="center"
                android:textColor="@color/black"
                android:textSize="24sp" />

            <TextView
                android:clickable="true"
                android:id="@+id/type"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginBottom="10dp"
                android:layout_marginRight="10dp"
                android:gravity="center"
                android:text="@{user.type ?? @string/app_name}"
                android:textColor="@color/black"
                android:textSize="24sp" />
        </LinearLayout>

    </android.support.v7.widget.CardView>
</layout>


有的朋友可能不太理解 android:text="@{user.type ?? @string/app_name}" 这句,它的意思是变量user不为空时,这里显示的是user的type值,否则显示string中的app_name值,如图


在加入<layout>标签后,会自动生成一个Binding类,如listview_userlist.xml生成的就是 ListviewUserlistBinding类,命名规则是,xml文件的名字去掉下划线,后面加上Binding,采用驼峰法命名。

我们可以使用

ListviewUserlistBinding binding = DataBindingUtil.setContentView(this , R.layout.listview_userlist);
这句话来绑定布局文件,相比于以前常用的setContentView()方法,采用这种方式绑定后,这个布局文件下的所有注册id的控件都可以直接取到,不必再使用繁复的findViewById方法。如listview_userlist.xml文件中的ListView控件,就可以直接使用

binding.lvUserlist.setLayoutManager(new LinearLayoutManager(this));

-----------------------------------------可耻的分割线---------------------------------------------

向RecyclerView中添加数据同样需要Adapter

public abstract class MyAdapter<T> extends RecyclerView.Adapter<ItemViewHolder>{
    protected List<T> mDatas;
    protected int mLayoutId;
    protected int mVariableId;

    public MyAdapter(int layoutId , int variableId , List<T> datas) {
        mDatas = datas;
        mVariableId = variableId;
        mLayoutId = layoutId;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        convert(holder , mDatas.get(position));
    }

    public abstract void convert(ItemViewHolder holder, T t);

    @Override
    public int getItemCount() {
        return mDatas.size();
    }
}

ItemViewHolder

public class ItemViewHolder extends RecyclerView.ViewHolder {

    private ViewDataBinding mBinding;

    public ItemViewHolder(View v) {
        super(v);
        mBinding = DataBindingUtil.bind(v);
    }

    public ItemViewHolder setBinding(int variableId , Object object){
        mBinding.setVariable(variableId , object);
        mBinding.executePendingBindings();
        return this;
    }
}

实际使用时可以这样调用:

binding.lvUserlist.setLayoutManager(new LinearLayoutManager(this));

        final int variableId = BR.user;

        MyAdapter<User> myAdapter = new MyAdapter<User>(R.layout.user_item, variableId, lt_userList) {
            @Override
            public void convert(ItemViewHolder holder, User user) {
                holder.setBinding(variableId , user);
            }
        };

        binding.lvUserlist.setAdapter(myAdapter);

其中serBinding方法使用了Binding类中的setVariable()方法,为布局文件中<layout>标签下的<variable>赋值,完成数据的绑定。


博主暂时以TextView绑定为例,文章后续会更新,加入其它控件的使用。

如果有不足之处,还望大家不吝指教!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值