Android——MVVM架构实现数据刷新

简介: 效果图示例结构图代码解析导入dataBinding实体类显示图片实体类全部代码xml视图VM接收数据发送数据建立接口,回调数据制造数据绑定视图与数据层


效果图



image.png

示例结构图



image.png


代码解析


导入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>
————————————————
版权声明:本文为CSDN博主「FranzLiszt1847」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/News53231323/article/details/120998933

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() );
    }
}


相关文章
|
2月前
|
存储 BI Shell
Doris基础-架构、数据模型、数据划分
Apache Doris 是一款高性能、实时分析型数据库,基于MPP架构,支持高并发查询与复杂分析。其前身是百度的Palo项目,现为Apache顶级项目。Doris适用于报表分析、数据仓库构建、日志检索等场景,具备存算一体与存算分离两种架构,灵活适应不同业务需求。它提供主键、明细和聚合三种数据模型,便于高效处理更新、存储与统计汇总操作,广泛应用于大数据分析领域。
366 2
|
1月前
|
数据采集 缓存 前端开发
如何开发门店业绩上报管理系统中的商品数据板块?(附架构图+流程图+代码参考)
本文深入讲解门店业绩上报系统中商品数据板块的设计与实现,涵盖商品类别、信息、档案等内容,详细阐述技术架构、业务流程、数据库设计及开发技巧,并提供完整代码示例,助力企业构建稳定、可扩展的商品数据系统。
|
7天前
|
数据采集 机器学习/深度学习 搜索推荐
MIT新论文:数据即上限,扩散模型的关键能力来自图像统计规律,而非复杂架构
MIT与丰田研究院研究发现,扩散模型的“局部性”并非源于网络架构的精巧设计,而是自然图像统计规律的产物。通过线性模型仅学习像素相关性,即可复现U-Net般的局部敏感模式,揭示数据本身蕴含生成“魔法”。
41 3
MIT新论文:数据即上限,扩散模型的关键能力来自图像统计规律,而非复杂架构
|
2月前
|
SQL 缓存 前端开发
如何开发进销存系统中的基础数据板块?(附架构图+流程图+代码参考)
进销存系统是企业管理采购、销售与库存的核心工具,能有效提升运营效率。其中,“基础数据板块”作为系统基石,决定了后续业务的准确性与扩展性。本文详解产品与仓库模块的设计实现,涵盖功能概述、表结构设计、前后端代码示例及数据流架构,助力企业构建高效稳定的数字化管理体系。
|
17天前
|
JSON 供应链 监控
1688商品详情API技术深度解析:从接口架构到数据融合实战
1688商品详情API(item_get接口)可通过商品ID获取标题、价格、库存、SKU等核心数据,适用于价格监控、供应链管理等场景。支持JSON格式返回,需企业认证。Python示例展示如何调用接口获取商品信息。
|
1月前
|
数据采集 监控 数据可视化
数据量暴涨时,抓取架构该如何应对?——豆瓣电影案例调研
本案例讲述了在豆瓣电影数据采集过程中,面对数据量激增和限制机制带来的挑战,如何通过引入爬虫代理、分布式架构与异步IO等技术手段,实现采集系统的优化与扩展,最终支撑起百万级请求的稳定抓取。
数据量暴涨时,抓取架构该如何应对?——豆瓣电影案例调研
|
1月前
|
缓存 前端开发 BI
如何开发门店业绩上报管理系统中的门店数据板块?(附架构图+流程图+代码参考)
门店业绩上报管理是将门店营业、动销、人效等数据按标准化流程上报至企业中台或BI系统,用于考核、分析和决策。其核心在于构建“数据底座”,涵盖门店信息管理、数据采集、校验、汇总与对接。实现时需解决数据脏、上报慢、分析无据等问题。本文详解了实现路径,包括系统架构、数据模型、业务流程、开发要点、三大代码块(数据库、后端、前端)及FAQ,助你构建高效门店数据管理体系。
|
2月前
|
数据采集 存储 分布式计算
一文读懂数据中台架构,高效构建企业数据价值
在数字化时代,企业面临数据分散、难以统一管理的问题。数据中台架构通过整合、清洗和管理数据,打破信息孤岛,提升决策效率。本文详解其核心组成、搭建步骤及常见挑战,助力企业高效用数。
1026 24
|
1月前
|
SQL 数据采集 数据处理
终于有人把数据架构讲清楚了!
本文深入浅出地解析了数据架构的核心逻辑,涵盖其定义、作用、设计方法及常见误区,助力读者构建贴合业务的数据架构。
|
5月前
|
存储 运维 Serverless
千万级数据秒级响应!碧桂园基于 EMR Serverless StarRocks 升级存算分离架构实践
碧桂园服务通过引入 EMR Serverless StarRocks 存算分离架构,解决了海量数据处理中的资源利用率低、并发能力不足等问题,显著降低了硬件和运维成本。实时查询性能提升8倍,查询出错率减少30倍,集群数据 SLA 达99.99%。此次技术升级不仅优化了用户体验,还结合AI打造了“一看”和“—问”智能场景助力精准决策与风险预测。
506 69

热门文章

最新文章