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


相关文章
|
3月前
|
安全 Android开发 iOS开发
深入探索Android与iOS的差异:从系统架构到用户体验
在当今的智能手机市场中,Android和iOS无疑是最受欢迎的两大操作系统。本文旨在探讨这两个平台之间的主要差异,包括它们的系统架构、开发环境、安全性、以及用户体验等方面。通过对比分析,我们可以更好地理解为何不同的用户群体可能会偏好其中一个平台,以及这些偏好背后的技术原因。
|
3月前
|
消息中间件 存储 缓存
十万订单每秒热点数据架构优化实践深度解析
【11月更文挑战第20天】随着互联网技术的飞速发展,电子商务平台在高峰时段需要处理海量订单,这对系统的性能、稳定性和扩展性提出了极高的要求。尤其是在“双十一”、“618”等大型促销活动中,每秒需要处理数万甚至数十万笔订单,这对系统的热点数据处理能力构成了严峻挑战。本文将深入探讨如何优化架构以应对每秒十万订单级别的热点数据处理,从历史背景、功能点、业务场景、底层原理以及使用Java模拟示例等多个维度进行剖析。
79 8
|
5天前
|
存储 数据采集 人工智能
AllData数据中台架构全览:数据时代的智慧中枢
杭州奥零数据科技有限公司成立于2023年,专注于数据中台业务,维护开源项目AllData并提供商业版解决方案。AllData提供数据集成、存储、开发、治理及BI展示等一站式服务,支持AI大模型应用,助力企业高效利用数据价值。
|
2月前
|
网络协议 Linux Android开发
深入探索Android系统架构与性能优化
本文旨在为读者提供一个全面的视角,以理解Android系统的架构及其关键组件。我们将探讨Android的发展历程、核心特性以及如何通过有效的策略来提升应用的性能和用户体验。本文不包含常规的技术细节,而是聚焦于系统架构层面的深入分析,以及针对开发者的实际优化建议。
117 21
|
2月前
|
存储 Linux API
深入探索Android系统架构:从内核到应用层的全面解析
本文旨在为读者提供一份详尽的Android系统架构分析,从底层的Linux内核到顶层的应用程序框架。我们将探讨Android系统的模块化设计、各层之间的交互机制以及它们如何共同协作以支持丰富多样的应用生态。通过本篇文章,开发者和爱好者可以更深入理解Android平台的工作原理,从而优化开发流程和提升应用性能。
|
2月前
|
安全 Android开发 iOS开发
深入探索iOS与Android系统架构差异及其对开发者的影响
本文旨在通过对比分析iOS和Android两大移动操作系统的系统架构,探讨它们在设计理念、技术实现及开发者生态方面的差异。不同于常规摘要仅概述内容要点,本摘要将简要触及核心议题,为读者提供对两大平台架构特点的宏观理解,铺垫
|
3月前
|
IDE 安全 Android开发
深入探索Android与iOS操作系统的架构差异
本文旨在对比分析Android和iOS两大主流移动操作系统在架构设计上的根本差异。通过详细解读两者的系统架构、开发环境、以及安全性等方面,揭示它们各自的特点及优势,为开发者选择合适的平台提供参考。
|
2月前
|
开发工具 Android开发 iOS开发
Android与iOS生态差异深度剖析:技术架构、开发体验与市场影响####
本文旨在深入探讨Android与iOS两大移动操作系统在技术架构、开发环境及市场表现上的核心差异,为开发者和技术爱好者提供全面的视角。通过对比分析,揭示两者如何塑造了当今多样化的移动应用生态,并对未来发展趋势进行了展望。 ####
|
3月前
|
安全 Linux Android开发
深入探索Android与iOS的系统架构:一场技术较量
在当今数字化时代,智能手机操作系统的选择成为了用户和开发者关注的焦点。本文将深入探讨Android与iOS两大主流操作系统的系统架构,分析它们各自的优势与局限性,并对比两者在用户体验、开发生态和安全性方面的差异。通过本文的技术剖析,读者将对这两个平台的核心技术有更深入的理解。
|
3月前
|
安全 Java Linux
深入解析Android系统架构及其对开发者的意义####
【10月更文挑战第21天】 本文旨在为读者揭开Android操作系统架构的神秘面纱,探讨其如何塑造现代移动应用开发格局。通过剖析Linux内核、硬件抽象层、运行时环境及应用程序框架等关键组件,揭示Android平台的强大功能与灵活性。文章强调了理解Android架构对于开发者优化应用性能、提升用户体验的重要性,并展望了未来技术趋势下Android的发展方向。 ####
82 0

热门文章

最新文章

  • 1
    如何修复 Android 和 Windows 不支持视频编解码器的问题?
  • 2
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 3
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
  • 4
    【04】flutter补打包流程的签名过程-APP安卓调试配置-结构化项目目录-完善注册相关页面-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程
  • 5
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
  • 6
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 7
    Android经典面试题之Kotlin中Lambda表达式和匿名函数的区别
  • 8
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 9
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 10
    深入剖析Transformer架构中的多头注意力机制