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;
}
}