Android实现MVVM架构数据刷新详解流程
作者:FranzLiszt1847 发布时间:2023-07-05 13:33:41
标签:Android,数据刷新,MVVM架构
效果图
示例结构图
代码解析
导入dataBinding
dataBinding{
enabled = true
}
实体类
继承BaseObservable
public class Sensor extends BaseObservable
为字段添加@Bindable
@Bindable
public String getTmpValue() {
return tmpValue;
}
显示图片
图片添加@BindingAdapter
@BindingAdapter( "tmpImage" )
示例采用本地图片,没有采用网络图片
@BindingAdapter( "tmpImage" )
public static void setTmpImage(ImageView view, int tmpImage) {
view.setImageDrawable( view.getContext().getResources().getDrawable( tmpImage ) );
}
为图片路径绑定字段
@Bindable
public int getTmpImage() {
return tmpImage;
}
绑定实体类
<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” )设置里的内容,设置属性
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
tmpImage="@{viewmodel.sensor.tmpImage}"
android:scaleType="fitCenter"/>
实体类全部代码
public 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视图
<?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类的接口,接收传感器数据(随机数据)
private 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();
}
});
}
发送数据
建立接口,回调数据
public interface HandleData{
void getSensorValue(int[] value);
}
public void setHandleDta(HandleData handleDta){
int[] value = ReturnData();
handleDta.getSensorValue(value);
}
制造数据
private 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();
}
绑定视图与数据层
public 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() );
}
}
来源:https://blog.csdn.net/News53231323/article/details/120998933


猜你喜欢
- 1.注解声明:通过@interface就可以声明一个注解。@Target(ElementType.FIELD)@Retention(Rete
- //activity的xml<?xml version="1.0" encoding="utf-8&qu
- 强制下线是需要关闭所有的活动,先创建一个类来管理所有的活动。class ActivityCollector { //var ac
- PullToRefresh是一套实现非常好的下拉刷新库,它支持:1.ListView2.ExpandableListView3.GridVi
- 一.介绍Apache Shiro是Java的一个安全框架。目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring
- 一:引入依赖包<!-- https://mvnrepository.com/artifact/org.springframework.
- 最近接了一个项目其中有功能要实现一个清理内存,要求和微信的效果一样。于是想到用surfaceView而不是继承view。下面小编给大家解析下
- 本文实例讲述了Android编程使用GestureDetector实现简单手势监听与处理的方法。分享给大家供大家参考,具体如下:添加手势识别
- 一、介绍Properties文件在Java中主要为配置文件,文件类型为:.properties,格式为文本文件,内容格式为"键=值
- 本文实例讲述了java识别一篇文章中某单词出现个数的方法。分享给大家供大家参考。具体如下:1. java代码:import java.io.
- 本文实例讲述了Android实现使用微信登录第三方APP的方法。分享给大家供大家参考,具体如下:使用微信登录APP,免去注册过程,现在已经有
- 目录一、什么是反射:二、反射的原理:三、反射的优缺点:四、反射的用途:五、反射机制常用的类:六、反射的基本使用:1、获得Class:主要有三
- 多态性1理解多态性:可以理解为一个事物的多种形态。2何为多态性:对象的多态性:父类的引用指向子类的对象(或子类的对象赋给父类的引用)3多态的
- 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;
- 下面一段代码准确的介绍了java实现单链表逆序,具体内容就不做详解了,有需要的朋友可以直接拷贝了package com.ckw.miansh
- 接着上一篇继续,老铁们1.检查数组的有序性给定一个整型数组, 判断是否该数组是有序的(升序) public static bo
- 本文实例为大家分享了javaweb购物车案列的具体代码,供大家参考,具体内容如下一、项目目录结构 二、源代码dao包——dao层:
- 以下教程是小编在参与开发公司的一个crm系统,整理些相关资料,在该系统中有很多消息推送功能,在其中用到了websocket技术。下面小编整理
- 封装在如何理解面向对象这篇文章中,提到所谓的封装就是“功能都给你做好了,你不必去理解它是怎么写出来的,直接使用即可。”。但你得清楚一点,那就
- 1.SpringBoot整合JDBCTemplate1.1.导入jdbc相关依赖包主要的依赖包:<dependency> &nb