首先介绍一种新的实体类的写法(当数据发生变化的时候,ui界面也会发生更新)
public class User {
public ObservableField<String> name = new ObservableField<>();
public ObservableField<Integer> age = new ObservableField<>();
}
在xml中的使用
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{`这个人的姓名是` + user.name}" />
在Activity中的使用(set方法发生了变化)
User user = new User();
user.name.set("wu");
user.age.set(i);
接下来进入正题,如何实现DataBinding和RecyclerView的使用
1.在Activity中添加RecyclerView控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.databindingproject.SecondActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
2.新建一个Item文件(列表的展现形式)
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="com.example.administrator.databindingproject.User" />
<variable
name="user"
type="User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#333333" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#ffffff"
android:orientation="horizontal"
android:paddingLeft="10dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@{`这个人的姓名是` + user.name}" />
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:text="@{String.valueOf(user.age)}" />
</LinearLayout>
</LinearLayout>
</layout>
3.新建一个Adapter
package com.example.administrator.databindingproject;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.administrator.databindingproject.databinding.ItemSecondBinding;
import java.util.List;
/**
* Created by $wu on 2017-09-09 上午 10:40.
*/
public class SecondAdapter extends RecyclerView.Adapter {
private List<User> users;
private Context context;
public SecondAdapter(List<User> users, Context context) {
if(this.users == null){
this.users = new ArryList<User>();
}
this.users = users;
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
ItemSecondBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_second, parent, false);
return new MyViewHolder(binding.getRoot());
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ItemSecondBinding binding = DataBindingUtil.getBinding(holder.itemView);
binding.setUser(users.get(position));
binding.executePendingBindings();
}
@Override
public int getItemCount() {
return users.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View itemView) {
super(itemView);
}
}
}
4.在Activity中为RecyclerView设置数据
private void initView() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<User> users = new ArrayList<>();
for (int i = 0; i < 30; i++) {
User user = new User();
user.name.set("wu");
user.age.set(i);
users.add(user);
}
SecondAdapter adapter = new SecondAdapter(users, this);
recyclerView.setAdapter(adapter);
}
之前的文章介绍了DataBinding的基本使用http://www.jianshu.com/p/da4e72a9885c