依赖

implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

android.arch.lifecycle:extensions:1.1.1用于引用ViewModelProviders

ViewModelProviders官方文档

EyeKeyService.java

package com.bu_ish.viewmodeltest.service;

import com.bu_ish.viewmodeltest.reply.FaceCheckingReply;
import com.bu_ish.viewmodeltest.reply.FacesComparingReply; import io.reactivex.Single;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query; public interface EyeKeyService {
String BASE_URL = "http://api.eyekey.com/face/";
String APP_ID = "f89ae61fd63d4a63842277e9144a6bd2";
String APP_KEY = "af1cd33549c54b27ae24aeb041865da2"; @FormUrlEncoded
@POST("Check/checking")
Single<FaceCheckingReply> checkFace(@Field("app_id") String appId, @Field("app_key") String appKey, @Field("url") String url); @GET("Match/match_compare")
Single<FacesComparingReply> compareFaces(@Query("app_id") String appId, @Query("app_key") String appKey, @Query("face_id1") String faceId1, @Query("face_id2") String faceId2);
}

EyeKeyRepository.java

package com.bu_ish.viewmodeltest.view_model;

import com.bu_ish.viewmodeltest.service.EyeKeyService;
import com.bu_ish.viewmodeltest.reply.FaceCheckingReply;
import com.bu_ish.viewmodeltest.reply.FacesComparingReply; import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory; public class EyeKeyRepository {
private static EyeKeyRepository instance = new EyeKeyRepository();
private EyeKeyService service; private EyeKeyRepository() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(EyeKeyService.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
service = retrofit.create(EyeKeyService.class);
} public static EyeKeyRepository getInstance() {
return instance;
} public Single<FaceCheckingReply> checkFace(String url) {
return setScheduler(service.checkFace(EyeKeyService.APP_ID, EyeKeyService.APP_KEY, url));
} public Single<FacesComparingReply> compareFaces(String faceId1, String faceId2) {
return setScheduler(service.compareFaces(EyeKeyService.APP_ID, EyeKeyService.APP_KEY, faceId1, faceId2));
} private Single setScheduler(Single single) {
return single.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
}

EyeKeyViewModel.java

package com.bu_ish.viewmodeltest.view_model;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel; import com.bu_ish.viewmodeltest.observer.ReplyObserver;
import com.bu_ish.viewmodeltest.reply.FaceCheckingReply;
import com.bu_ish.viewmodeltest.reply.FacesComparingReply; public class EyeKeyViewModel extends ViewModel {
public MutableLiveData<FaceCheckingReply> faceCheckingReply1Data = new MutableLiveData<>();
public MutableLiveData<FaceCheckingReply> faceCheckingReply2Data = new MutableLiveData<>();
public MutableLiveData<FacesComparingReply> facesComparingReplyData = new MutableLiveData<>();
private DialogViewModel dialogViewModel;
private EyeKeyRepository eyeKeyRepository; public EyeKeyViewModel(DialogViewModel dialogViewModel) {
this.dialogViewModel = dialogViewModel;
eyeKeyRepository = EyeKeyRepository.getInstance();
} public void checkFace1(String url) {
eyeKeyRepository.checkFace(url).subscribe(new ReplyObserver<FaceCheckingReply>(dialogViewModel) {
@Override
public void onReply(FaceCheckingReply reply) {
faceCheckingReply1Data.setValue(reply);
}
});
} public void checkFace2(String url) {
eyeKeyRepository.checkFace(url).subscribe(new ReplyObserver<FaceCheckingReply>(dialogViewModel) {
@Override
public void onReply(FaceCheckingReply reply) {
faceCheckingReply2Data.setValue(reply);
}
});
} public void compareFaces(String faceId1, String faceId2) {
eyeKeyRepository.compareFaces(faceId1, faceId2).subscribe(new ReplyObserver<FacesComparingReply>(dialogViewModel) {
@Override
public void onReply(FacesComparingReply reply) {
facesComparingReplyData.setValue(reply);
}
});
}
}

ReplyObserver.java

package com.bu_ish.viewmodeltest.observer;

import com.bu_ish.viewmodeltest.reply.BaseReply;
import com.bu_ish.viewmodeltest.view_model.DialogViewModel; import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable; public abstract class ReplyObserver<T extends BaseReply> implements SingleObserver<T> {
public abstract void onReply(T reply); private DialogViewModel dialogViewModel; public ReplyObserver(DialogViewModel dialogViewModel) {
this.dialogViewModel = dialogViewModel;
} @Override
public void onSubscribe(Disposable d) {
dialogViewModel.showProgressData.setValue(true);
} @Override
public void onSuccess(T reply) {
dialogViewModel.showProgressData.setValue(false);
String code = reply.getResCode();
if (code.equals("0000")) {
onReply(reply);
} else {
dialogViewModel.showErrorData.setValue(reply);
}
} @Override
public void onError(Throwable e) {
dialogViewModel.showProgressData.setValue(false);
dialogViewModel.showExceptionData.setValue(e);
}
}

EyeKeyViewModelFactory.java

package com.bu_ish.viewmodeltest.view_model;

import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.support.annotation.NonNull; public class EyeKeyViewModelFactory implements ViewModelProvider.Factory {
private DialogViewModel dialogViewModel; public EyeKeyViewModelFactory(DialogViewModel dialogViewModel) {
this.dialogViewModel = dialogViewModel;
} @NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new EyeKeyViewModel(dialogViewModel);
}
}

DialogViewModel.java

package com.bu_ish.viewmodeltest.view_model;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel; import com.bu_ish.viewmodeltest.reply.BaseReply; public class DialogViewModel extends ViewModel {
public MutableLiveData<Boolean> showProgressData = new MutableLiveData<>();
public MutableLiveData<BaseReply> showErrorData = new MutableLiveData<>();
public MutableLiveData<Throwable> showExceptionData = new MutableLiveData<>();
}

使用ViewModel的好处

即使发生配置变更(Configuration Change),ViewModel中的数据依然存在

所以,使用ViewModel后,也就不需要考虑onSaveInstanceState这类操作了^_^

参考博文

MVVM 架构,ViewModel和LiveData(一)

完整Demo

链接:https://pan.baidu.com/s/1TwY9OZTaQPk1eCn7vSz4fA,提取码:x0fy

Android笔记之ViewModel的使用示例的更多相关文章

  1. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  2. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  3. [置顶] Xamarin android如何调用百度地图入门示例(一)

    在Xamarin android如何调用百度地图呢? 首先我们要区分清楚,百度地图这是一个广泛的概念,很多刚刚接触这个名词"百度地图api",的确是泛泛而谈,我们来看一下百度地图的 ...

  4. Android判断当前网络是否可用--示例代码

    Android判断当前网络是否可用--示例代码 分类: *07 Android 2011-05-24 13:46 7814人阅读 评论(4) 收藏 举报 网络androiddialogmanagern ...

  5. Android 笔记之 R 文件

    Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...

  6. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  7. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

  8. Android笔记--View绘制流程源码分析(二)

    Android笔记--View绘制流程源码分析二 通过上一篇View绘制流程源码分析一可以知晓整个绘制流程之前,在activity启动过程中: Window的建立(activit.attach生成), ...

  9. Android笔记--View绘制流程源码分析(一)

    Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...

随机推荐

  1. Codeforces 323C Two permutations

    题目描述 You are given two permutations pp and qq , consisting of nn elements, and mm queries of the for ...

  2. Displaying Tabbed and Stacked Canvas Using Show_View In Oracle Forms

    Displays the indicated canvas at the coordinates specified by the canvas's X Position and Y Position ...

  3. dedecms 调用父栏目下的所有子栏目

    效果如下: 代码如下: <div class="productxilie"> <ul> {dede:channelartlist row=6 typeid ...

  4. MATLAB逻辑函数

    %%逻辑函数 %%all:判断是否有元素非0,A是多维矩阵,all(A)是以列为单位来处理的,当前列的逻辑 %值为1,当且仅当当前列的每一个元素都非0 A=[1,2,3;0,2,1;5,0,2]; % ...

  5. MyBatis-Invalid bound statement (not found)-问题处理

    最近把工程改为Hibernate和MyBatis并存,并存只要注意两点即可: 1.使用同一个dataSource 2.事物交给Hibernate进行管理(Hibernate4+)  Hibernate ...

  6. DB2和MySQL常用SQL整理

    1.Truncate删除表中所有数据 truncate table USER immediate; 说明:Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段 ...

  7. linux中nl用法

    linux 中nl 命令使用 nl :添加行号打印 -b:   指定行号指定的方式,主要有两种:    -b a : 表示不论是否为空行,都同样列出行号    -b t : 如果有空行,则不列出那一行 ...

  8. Predicate与filter

    转: http://blog.csdn.net/michaellufhl/article/details/6329823 怎么根据某些条件来过滤Collection的元素?我们可以在循环里面判断元素是 ...

  9. Solidworks如何绘制螺纹

    1 随便画一个圆柱   2 在原来的地方画一个一摸一样的圆(草图2)   3 在特征选项卡中点击曲线-螺旋线/涡状线   4 设置螺距和圈数,画螺旋线   5 建立一个基准面,第一参考是点,第二参考是 ...

  10. 微信小程序 - 传参的几种方式

    1. navigator navigator?第一参数&第二参数 .... 在传递页面的options可以拿到传递过来的参数 <navigator url='start-test/sta ...