Android进阶---android databinding 使用

本文介绍了Android Data Binding的配置步骤,包括在模块中添加依赖,创建Model实体类,改造布局文件,以及在Activity中绑定使用。讲解了Data Binding的基础用法,如何自动更新UI,并详细阐述了API文档中的知识,如数据关联、变量定义、事件处理。同时,还探讨了在RecyclerView中应用Data Binding的方法,包括RecyclerItemBinding的生成和Adapter的创建。
摘要由CSDN通过智能技术生成

1.配置,

  • 在 module里面添加
android {   
     ....   
     dataBinding {        
       enabled = true   
     }
}

2.基础使用

  • 创建简单的model实体类
public class SwordMan{
    private String name;
    private String level;

    public SwordMan(String name, String level) {
        this.name = name;
        this.level = level;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;

    }
}
  • 编写布局文件:需要根布局变成layout
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="swordsman"
            type="com.light.testdatabinding.SwordMan" />
    </data>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={swordsman.name}" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{swordsman.leval}" />
    </LinearLayout>

</layout>
  • activty里面绑定使用
ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
SwordMan swordMan=new SwordMan("你好","点击我");
binding.setSwordsman(swordMan);
  • 补充

    以上就是databinding基础使用,我们给实体类赋值以后textview的text会自动改变。ActivityMainBinding是根据绑定的布局名称自动生成的。它的作用就相当于布局一个替代类

3.知识讲解,详细介绍阅读API文档

  • 根布局layout替代,里面的布局不变
  • data节点用来关联定义数据,里面的variable节点(一个节点相当于一个实体类):name属性表示变量的名作,type表示变量的类型。import节点导入一个类的包,他的使用和type类似。如下。
  <data>
        <import type="java.util.Date"/>

        <variable
            name="swordsman"
            type="com.light.testdatabinding.SwordMan" />
        <variable
            name="time"
            type="Date" />
    </data>
  • 当我们绑定一个布局以后,就可以通过对象直接获取id:test就是布局里面textview的id
ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
binding.test.setText("测试");
  • 事件处理:直接通过binding对象操作id
 binding.btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                swordMan.setName("重置");
                startActivity(new Intent(MainActivity.this,Main2Activity.class));
            }
        });

4.recyclerview使用databinding

  • recyclerviewr布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>
    </LinearLayout>

</layout>
  • recyclerviewr item布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <import type="com.light.testdatabinding.OBSwordMan" />

        <variable
            name="swordman"
            type="OBSwordMan" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{swordman.name}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{swordman.level}" />
    </LinearLayout>

</layout>
  • 实体类

public class OBSwordMan extends BaseObservable {
    private String name;
    private String level;

    public OBSwordMan(String name, String level) {
        this.name = name;
        this.level = level;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;

    }
}
  • recyclerview Adapter创建,RvItemBinding类是更具布局名生成的类

public class SwordmanAdapter extends RecyclerView.Adapter<SwordmanAdapter.SwordmamViewHoler> {
    private List<OBSwordMan> manList;

    public SwordmanAdapter(List<OBSwordMan> manList) {
        this.manList = manList;
    }

    @Override
    public SwordmamViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        RvItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.rv_item, parent, false);
        return new SwordmamViewHoler(binding);
    }

    @Override
    public void onBindViewHolder(SwordmamViewHoler holder, int position) {
        OBSwordMan swordMan = manList.get(position);
        holder.getBinding().setSwordman(swordMan);

    }

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

    public class SwordmamViewHoler extends RecyclerView.ViewHolder {
        RvItemBinding binding;

        public SwordmamViewHoler(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = (RvItemBinding) binding;
        }

        public RvItemBinding getBinding() {
            return binding;
        }
    }
}
  • activity里面实现
public class Main2Activity extends AppCompatActivity {
    private ActivityMain2Binding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main2);
        initRecycler();

    }

    private void initRecycler() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        binding.recycler.setLayoutManager(manager);
        SwordmanAdapter adapter = new SwordmanAdapter(getList());
        binding.recycler.setAdapter(adapter);
    }

    public List<OBSwordMan> getList() {
        List<OBSwordMan> list = new ArrayList<>();
        OBSwordMan obSwordMan = new OBSwordMan("张三", "生命如此短暂");
        OBSwordMan obSwordMan2 = new OBSwordMan("张三1", "生命如此短暂");
        OBSwordMan obSwordMan3 = new OBSwordMan("张三2", "生命如此短暂");
        OBSwordMan obSwordMan4 = new OBSwordMan("张三3", "生命如此短暂");
        OBSwordMan obSwordMan5 = new OBSwordMan("张三4", "生命如此短暂");
        list.add(obSwordMan);
        list.add(obSwordMan2);
        list.add(obSwordMan3);
        list.add(obSwordMan4);
        list.add(obSwordMan5);
        return list;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值