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中去。
在需要应用DataBinding的Module的gradle文件中添加:
2
3
1
2
3
即可。
在原本的layout布局文件中将最外层的布局标签替换为:
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标签,用于指定要绑定的数据类:
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了。
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方法
2
3
4
5
1
2
3
4
5
在布局文件中直接声明方法的引用:
2
3
4
5
6
1
2
3
4
5
6
监听器绑定:
也是要先在Presenter中定义需要的方法,并定义好参数:
2
3
4
5
6
1
2
3
4
5
6
只是在布局文件中引用的时候有所不同:
2
3
4
5
6
1
2
3
4
5
6
这样就完成了监听器的绑定。
最新最新推出的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
这样就完成了监听器的绑定。
相关文章推荐
- Google官方支持的MVVM架构框架Data Binding使用入门
- MvvmLight框架使用入门(三)
- MvvmLight框架使用入门(一)
- google官方架构MVP解析与实战-(从零开始搭建android框架系列(3))
- MvvmLight框架使用入门(四)
- MvvmLight框架使用入门(三)
- Google官方网络框架-Volley的使用解析Json以及加载网络图片方法
- ReactiveCocoa框架菜鸟入门(二)——MVVM架构与ReactiveCocoa框架
- Android-MVVM架构-Data Binding的使用
- MvvmLight框架使用入门(一)
- MvvmLight框架使用入门(三)
- Android-MVVM架构-Data Binding的使用
- ReactiveCocoa框架菜鸟入门(二)——MVVM架构与ReactiveCocoa框架
- Android-MVVM架构-Data Binding的使用
- google官方架构MVP解析与实战-(从零开始搭建android框架系列(3))
- google官方架构MVP解析与实战-(从零开始搭建android框架系列(3))
- Android 官方数据绑定框架 Data Binding 简单使用
- Android-MVVM架构-Data Binding的使用
- Google官方MVP+Dagger2架构详解【从零开始搭建android框架系列(6)】
- MvvmLight框架使用入门(四)