欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现MVVM架构数据刷新详解流程

程序员文章站 2022-06-16 23:49:29
目录效果图示例结构图代码解析导入databinding实体类xml视图vm绑定视图与数据层效果图示例结构图代码解析导入databinding databinding{ enabl...

效果图

Android实现MVVM架构数据刷新详解流程

示例结构图

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

到此这篇关于android实现mvvm架构数据刷新详解流程的文章就介绍到这了,更多相关android 数据刷新内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!