您的位置:首页 > 运维架构 > 网站架构

Google官方支持的MVVM架构框架Data Binding使用入门

2017-03-12 16:16 441 查看
转载地址:http://blog.csdn.net/wqc_csdn/article/details/53399737

最新最新推出的MVVM架构是将MVP中的Presenter换成了View Model,也就是Model +View+ViewModel的模式。这种架构方式可以很便捷的进行数据的交互,当数据发生变化时通过ViewModel可以及时的将变化反应到View中去。


本文参考自:http://www.imooc.com/learn/719


http://www.imooc.com/learn/720


DataBinding的引入:

在需要应用DataBinding的Module的gradle文件中添加:
dataBinding{
enabled = true
}
1
2
3
1
2
3

即可。


DataBinding的使用:

在原本的layout布局文件中将最外层的布局标签替换为:
< layout >
//这里边是原本的布局文件
< / layout>

//例如
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="item"
type="com.wei.rxjavademo.Item"/>

<variable
name="presenter"
type="com.wei.rxjavademo.MainActivity.Presenter"/>
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
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"
tools:context="com.wei.rxjavademo.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{item.content}"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="订阅"/>

<Button
android:onClick="@{() -> presenter.onClickListenerBinding(item)}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消订阅"/>

<EditText
android:onTextChanged="@{presenter.onTextChanged}"
android:layout_width="match_parent"
android:layout_height="50dp"/>
</LinearLayout>
</layout>
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
40
41
42
43
44
45
46
47
48
49
50
51
52
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
40
41
42
43
44
45
46
47
48
49
50
51
52

同时在布局文件中加入data标签,用于指定要绑定的数据类:
<data>
<variable
name=""
type=""/>

</data>

//例如

<data>
<variable
name="item"
type="com.wei.rxjavademo.Item"/>
<!--name可以随意根据需要命名,type为该类型的全类名-->

<variable
name="presenter"
type="com.wei.rxjavademo.MainActivity.Presenter"/>
</data>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

修改完布局文件之后就可以在Java代码中使用DataBinding了。
mItem = new Item(12, "hello");
//AS会为每一个layout标签自动生成一个Binding类,用于绑定视图
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

//向布局文件中的variable设置变量
activityMainBinding.setItem(mItem);
activityMainBinding.setPresenter(new Presenter());
1
2
3
4
5
6
7
1
2
3
4
5
6
7

Item只是自定义的一个JavaBean,里边只有Id和Content两个属性及其get和set方法

以上就是DataBinding的数据的绑定,接下来还有方法的绑定。

方法的绑定有两种情况:

方法引用绑定:主要是绑定一些已经原有的方法事件,例如onClick,onTextChanged这些方法

监听器绑定:主要是绑定一些自定义的方法事件,可以支持传入数据类型。

方法引用绑定:

自定义一个Presenter类,并在其中实现onClick,onTextChanged等需要的方法,然后在布局文件中直接声明。

在Java中实现onClick方法
public class Presenter {
public void onClick(View view){
Toast.makeText(MainActivity.this, "onClick点击", Toast.LENGTH_SHORT).show();
}
}
1
2
3
4
5
1
2
3
4
5

在布局文件中直接声明方法的引用:
<Button
android:onClick="@{presenter.onClick}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="订阅"/>
<!--在onClick方法中直接声明引用的方法为Presenter中的onClick方法-->
1
2
3
4
5
6
1
2
3
4
5
6

监听器绑定:

也是要先在Presenter中定义需要的方法,并定义好参数:
public class Presenter {
//在这个方法中可以添加需要的参数
public void onClickListenerBinding(Item item){
Toast.makeText(MainActivity.this, item.getContent(), Toast.LENGTH_SHORT).show();
}
}
1
2
3
4
5
6
1
2
3
4
5
6

只是在布局文件中引用的时候有所不同:
<Button
android:onClick="@{() -> presenter.onClickListenerBinding(item)}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消订阅"/>
<!--在布局文件中的使用方式为Lambda表达式-->
1
2
3
4
5
6
1
2
3
4
5
6

这样就完成了监听器的绑定。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android binding mvp