栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

效果图 示例结构图 代码解析 导入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;
    }

绑定实体类



    
        
    
    

根据设置@BindingAdapter( “tmpImage” )设置里的内容,设置属性


实体类全部代码
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视图



    
        
    




   
    

        
            
            
            
        
    
        
        
        
    
    
        
        
        
    
    
        
        
        
    
    
        
        
        
    
    

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() );
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/350941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号