文章目录
  1. 1. Android实现事件DataBinding步骤
    1. 1.1. 一. 实现事件处理类
    2. 1.2. 二. 控件选择处理方法
    3. 1.3. 三. 实现控件和事件的绑定
  2. 2. Android使用DataBinding更新数据

上文实现控件和数据源的绑定,达到UI和业务逻辑分离,大大简化了代码,而且实现和解耦。本文讲主要讲解DataBinding事件的绑定和数据的实时更新。

Android实现事件DataBinding步骤

下面内容需要参考之前文章(传送门)。需要了解环境配置,绑定步骤等方面。

一. 实现事件处理类

编写事件处理类,代码如下:

1
2
3
4
5
6
7
8
9
10
11
public class MyHandlers {

public final void onClickName(View view) {
Toast.makeText(view.getContext(), "onClickName()", Toast.LENGTH_SHORT).show();
}

public final void onClickAge(View view) {
Toast.makeText(view.getContext(), "onClickAge()", Toast.LENGTH_SHORT).show();
}

}

二. 控件选择处理方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">

<data>
<variable name="myHandlers" type="com.ihongqiqu.databinding.event.MyHandlers"/>
<variable name="user" type="com.ihongqiqu.databinding.data.User" />
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@{user.name}"
android:onClick="@{myHandlers.onClickName}"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceMedium" />


<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="27dp"
android:text="@{user.age}"
android:onClick="@{myHandlers.onClickAge}"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceMedium" />


</LinearLayout>
</layout>

三. 实现控件和事件的绑定

Activityd的onCreate()方法添加绑定代码:

这一步很重要,不实现这一步会导致事件无法出发。

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("事件绑定");

User user = new User("jingle1267", "20");
MyHandlers myHandlers = new MyHandlers();

ActivityDataBindingSample2Binding binding = DataBindingUtil.setContentView(this,
R.layout.activity_data_binding_sample2);
binding.setUser(user);
binding.setMyHandlers(myHandlers);
}

👌OK,通过上述三个步骤就实现了事件的绑定!

Android使用DataBinding更新数据

实现数据实时更新,需要在在之前的基础上,额外实现一部分操作。

POJO需要做一下修改,修改后如下,重点是@BindablenotifyPropertyChanged(BR.XXX);,其中BR.XXX为对应需要更新的ID。具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 猪,有标签和重量两个属性
* <p/>
* Created by zhenguo on 3/13/16.
*/

public class Pig extends BaseObservable {

private String label;
private String weight;

@Bindable
public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
notifyPropertyChanged(BR.label);
}

@Bindable
public String getWeight() {
return weight;
}

public void setWeight(String weight) {
this.weight = weight;
notifyPropertyChanged(BR.weight);
}
}

事件处理类需要调整如下(仅供参考),代码中直接修改了实体的属性就实现了对UI的更新:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 事件处理
* <p/>
* Created by zhenguo on 3/13/16.
*/

public class PigHandler {

DataBindingSampleActivity3 sampleActivity3;

public PigHandler(DataBindingSampleActivity3 sampleActivity3) {
this.sampleActivity3 = sampleActivity3;
}

public void onClick(View view) {
if (sampleActivity3 != null && sampleActivity3.pig != null) {
sampleActivity3.pig.setLabel("长白山");
sampleActivity3.pig.setWeight("280Kg");
}
}

}

实现上述几个步骤,我们已经能够处理事件和实现数据更新。

源码地址https://github.com/jingle1267/DataDindingSample

参考网址: 官方介绍Data Binding Guide


本文地址 http://ihongqiqu.com/2016/03/14/Android-DataBinding-Part-Two/ 作者为 Zhenguo

author:Zhenguo
Author: Zhenguo      Blog: http://ihongqiqu.com/     Email: jinzhenguo1990@gmail.com
I have almost 6 years of Android application development experience and have a keen interested in the latest emerging technologies. I use my spare time to turn my experience, ideas and love for Android tech into informative articles, tutorials and more in hope to help others and learn more.
文章目录
  1. 1. Android实现事件DataBinding步骤
    1. 1.1. 一. 实现事件处理类
    2. 1.2. 二. 控件选择处理方法
    3. 1.3. 三. 实现控件和事件的绑定
  2. 2. Android使用DataBinding更新数据
返回顶部