DataBinding数据绑定

作者: 小瓜子儿姑娘 | 来源:发表于2017-05-12 17:23 被阅读67次

1、什么是数据绑定?
写布局文件的时候,我们用到的layout都是视图
现在推出一种新的layout,在layout中写两个字元素
一个字元素是:<data>
另一个字元素是:其他的控件,如<LinearLayout>等
在data中写数据模型,在LinearLayout(其他的控件)中写对于数据模型的引用,这样,数据和模型就放到了一起,于是就成了MVVM_VM

MVVM——model、view、viewmodel

2、如何数据绑定?
在build.gradle中的android中写:

dataBinding{
      enable = true;
}  

用来启动Google DataBinding

注意:数据绑定不能绑定图片!!!!

3、举例
启动Google DataBinding,在recyclerView中展示数据
(1)添加recyclerView的依赖

compile 'com.android.support:recyclerview-v7:23.4.0'

(2)添加数据绑定
在build.gradle中的android中写:

dataBinding{
    enabled = true
}

(3)activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ws.demo3.MainActivity">

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

(4)MainActivity.java

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);  
           //将recyclerView添加到布局管理器中
        recyclerView.setLayoutManager(linearLayoutManager);
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 200; i++) {
            //创建一个Student对象并进行设置
            Student student = new Student();
            student.setName("名字"+i);
            student.setAge("年龄"+i);
            student.setStuId("学号"+i);
            list.add(student);
        }
        adapter = new MyAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.setList(list);
    }
}

(5)Student.java

public class Student {
    private String name;
    private String age;
    private String stuId;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getStuId() {
        return stuId;
    }
    public void setStuId(String stuId) {
        this.stuId = stuId;
    }
}

(6)MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

    private Context context;
    private List<Student> list;
     //构造方法
    public MyAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setList(List<Student> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        ItemBinding itemBinding = DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.item,parent,false);
        MyViewHolder holder = new MyViewHolder(itemBinding);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position){
        //执行绑定
        holder.itemBinding.setStu(list.get(position));
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{

        ItemBinding itemBinding = null;
        public MyViewHolder(ItemBinding itemBinding){
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }
    }
}

(7)item.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
             >
    <!--type是完全路径名-->
    <data>
        <variable
            name="stu"
            type="com.ws.demo3.Student"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        >
          <!--引用数据模型的数据的时候要用@{}的形式-->
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@{stu.name}"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@{stu.age}"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@{stu.stuId}"
            />
    </LinearLayout>
</layout>

相关文章

网友评论

    本文标题:DataBinding数据绑定

    本文链接:https://www.haomeiwen.com/subject/efqtxxtx.html