效果图
示例结构图
代码解析
导入dataBinding
复制代码
1
2
3dataBinding{ enabled = true }
实体类
继承BaseObservable
复制代码
1public class Sensor extends BaseObservable
为字段添加@Bindable
复制代码
1
2
3
4@Bindable public String getTmpValue() { return tmpValue; }
显示图片
图片添加@BindingAdapter
复制代码
1@BindingAdapter( "tmpImage" )
示例采用本地图片,没有采用网络图片
复制代码
1
2
3
4@BindingAdapter( "tmpImage" ) public static void setTmpImage(ImageView view, int tmpImage) { view.setImageDrawable( view.getContext().getResources().getDrawable( tmpImage ) ); }
为图片路径绑定字段
复制代码
1
2
3
4@Bindable public int getTmpImage() { return tmpImage; }
绑定实体类
复制代码
1
2
3
4
5
6
7
8
9
10
11<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="viewmodel" type="com.franzliszt.refreshdata.viewmodel.ViewModel" /> </data> <layout/>
根据设置@BindingAdapter( “tmpImage” )设置里的内容,设置属性
复制代码
1
2
3
4
5
6<ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" tmpImage="@{viewmodel.sensor.tmpImage}" android:scaleType="fitCenter"/>
实体类全部代码
复制代码
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127public class Sensor extends BaseObservable { private String tmpValue; private String humValue; private String lightValue; private String humanValue; private String smokeValue; private String fireValue; private int tmpImage; private int humImage; private int lightImage; private int humanImage; private int smokeImage; private int fireImage; public Sensor(){ } public Sensor(int tmpImage,int humImage,int lightImage,int humanImage,int smokeImage,int fireImage){ this.tmpImage = tmpImage; this.humImage = humImage; this.lightImage = lightImage; this.humanImage = humanImage; this.smokeImage = smokeImage; this.fireImage = fireImage; } @Bindable public String getTmpValue() { return tmpValue; } public void setTmpValue(String tmpValue) { this.tmpValue = tmpValue; notifyPropertyChanged( BR.tmpValue ); } @Bindable public String getHumValue() { return humValue; } public void setHumValue(String humValue) { this.humValue = humValue; notifyPropertyChanged( BR.humValue ); } @Bindable public String getLightValue() { return lightValue; } public void setLightValue(String lightValue) { this.lightValue = lightValue; notifyPropertyChanged( BR.lightValue ); } @Bindable public String getHumanValue() { return humanValue; } public void setHumanValue(String humanValue) { this.humanValue = humanValue; notifyPropertyChanged( BR.humanValue ); } @Bindable public String getSmokeValue() { return smokeValue; } public void setSmokeValue(String smokeValue) { this.smokeValue = smokeValue; notifyPropertyChanged( BR.smokeValue ); } @Bindable public String getFireValue() { return fireValue; } public void setFireValue(String fireValue) { this.fireValue = fireValue; notifyPropertyChanged( BR.fireValue ); } @Bindable public int getTmpImage() { return tmpImage; } @BindingAdapter( "tmpImage" ) public static void setTmpImage(ImageView view, int tmpImage) { view.setImageDrawable( view.getContext().getResources().getDrawable( tmpImage ) ); } @Bindable public int getLightImage() { return lightImage; } @BindingAdapter( "lightImage" ) public static void setLightImage(ImageView view,int lightImage) { view.setImageDrawable( view.getContext().getResources().getDrawable( lightImage ) ); } @Bindable public int getHumanImage() { return humanImage; } @BindingAdapter( "humanImage" ) public static void setHumanImage(ImageView view,int humanImage) { view.setImageDrawable( view.getContext().getResources().getDrawable( humanImage ) ); } @Bindable public int getSmokeImage() { return smokeImage; } @BindingAdapter( "smokeImage" ) public static void setSmokeImage(ImageView view,int smokeImage) { view.setImageDrawable( view.getContext().getResources().getDrawable( smokeImage ) ); } @Bindable public int getFireImage() { return fireImage; } @BindingAdapter( "fireImage" ) public static void setFireImage(ImageView view,int fireImage) { view.setImageDrawable( view.getContext().getResources().getDrawable( fireImage ) ); } @Bindable public int getHumImage() { return humImage; } @BindingAdapter( "humImage" ) public static void setHumImage(ImageView view,int humImage) { view.setImageDrawable( view.getContext().getResources().getDrawable( humImage ) ); } }
xml视图
复制代码
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="viewmodel" type="com.franzliszt.refreshdata.viewmodel.ViewModel" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".view.MainActivity" android:layout_margin="30dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/item_bg_style" android:layout_marginTop="20dp" android:paddingTop="10dp"> <ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" tmpImage="@{viewmodel.sensor.tmpImage}" android:scaleType="fitCenter"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="温度值:" android:textColor="#ffffff" android:textSize="20sp" android:gravity="center"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="@{viewmodel.sensor.tmpValue}" android:textColor="#ffffff" android:textSize="25sp" android:gravity="center"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/item_bg_style" android:layout_marginTop="20dp" android:paddingTop="10dp"> <ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" humImage="@{viewmodel.sensor.humImage}" android:scaleType="fitCenter"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="湿度值:" android:textColor="#ffffff" android:textSize="20sp" android:gravity="center"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="@{viewmodel.sensor.humValue}" android:textColor="#ffffff" android:textSize="25sp" android:gravity="center"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/item_bg_style" android:layout_marginTop="20dp" android:paddingTop="10dp"> <ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" lightImage="@{viewmodel.sensor.lightImage}" android:scaleType="fitCenter"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="光照值:" android:textColor="#ffffff" android:textSize="20sp" android:gravity="center"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="@{viewmodel.sensor.lightValue}" android:textColor="#ffffff" android:textSize="25sp" android:gravity="center"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/item_bg_style" android:layout_marginTop="20dp" android:paddingTop="10dp"> <ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" smokeImage="@{viewmodel.sensor.smokeImage}" android:scaleType="fitCenter"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="烟雾值:" android:textColor="#ffffff" android:textSize="20sp" android:gravity="center"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="@{viewmodel.sensor.smokeValue}" android:textColor="#ffffff" android:textSize="25sp" android:gravity="center"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/item_bg_style" android:layout_marginTop="20dp" android:paddingTop="10dp"> <ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" fireImage="@{viewmodel.sensor.fireImage}" android:scaleType="fitCenter"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="火焰值:" android:textColor="#ffffff" android:textSize="20sp" android:gravity="center"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="@{viewmodel.sensor.fireValue}" android:textColor="#ffffff" android:textSize="25sp" android:gravity="center"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/item_bg_style" android:layout_marginTop="20dp" android:paddingTop="10dp"> <ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" humanImage="@{viewmodel.sensor.humanImage}" android:scaleType="fitCenter"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="人体红外:" android:textColor="#ffffff" android:textSize="20sp" android:gravity="center"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp" android:text="@{viewmodel.sensor.humanValue}" android:textColor="#ffffff" android:textSize="25sp" android:gravity="center"/> </LinearLayout> </LinearLayout> </layout>
VM
接收数据
调用Handle类的接口,接收传感器数据(随机数据)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22private void InitData(){ handle.setHandleDta( new Handle.HandleData() { @Override public void getSensorValue(int[] value) { new Thread( ()->{ while (true){ try { Thread.sleep( 5000 ); } catch (InterruptedException e) { e.printStackTrace(); } sensor.setTmpValue( value[0]+"℃"); sensor.setHumValue( value[1]+"RH" ); sensor.setLightValue( value[2]+"LUX" ); sensor.setSmokeValue( value[3]+"%" ); sensor.setFireValue( value[4]+"%" ); sensor.setHumanValue( value[5] == 1 ? "有人" : "无人" ); } } ).start(); } }); }
发送数据
建立接口,回调数据
复制代码
1
2
3
4
5
6
7public interface HandleData{ void getSensorValue(int[] value); } public void setHandleDta(HandleData handleDta){ int[] value = ReturnData(); handleDta.getSensorValue(value); }
制造数据
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25private void RefreshSensorValue(){ thread = new Thread( ()->{ while (true){ try { Thread.sleep( 2000 ); } catch (InterruptedException e) { e.printStackTrace(); } /*温度*/ value[0] = RandomRange(35,30); /*湿度*/ value[1] = RandomRange(80,75); /*光照值*/ value[2] = RandomRange(120,100); /*烟雾*/ value[3] = RandomRange(60,50); /*火焰*/ value[4] = RandomRange(30,25); /*红外*/ value[5] = RandomRange(2,0); Log.d( "TAG",value[5]+"" ); } } ); thread.start(); }
绑定视图与数据层
复制代码
1
2
3
4
5
6
7
8
9
10public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); binding = DataBindingUtil.setContentView( this,R.layout.activity_main ); binding.setViewmodel( new ViewModel() ); } }
到此这篇关于Android实现MVVM架构数据刷新详解流程的文章就介绍到这了,更多相关Android 数据刷新内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是阔达马里奥最近收集整理的关于Android实现MVVM架构数据刷新详解流程的全部内容,更多相关Android实现MVVM架构数据刷新详解流程内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复