在我看来,RxJava最大的特点就是异步,无论你是解析复杂的数据或是IO操作,我们都可以利用它内置的线程池进行线程间的调度,简单的使用

subscribeOn(Schedulers.io()).doOnNext(...)
observeOn(AndroidSchedulers.mainThread()).doOnNext(...)

这种操作就可以指定操作在你想要的线程里执行.

当然,网络请求这种耗时的操作肯定也是要放在子线程执行的,那么是异步操作,我们就会有等待时间,安卓里通常的做法是在界面上盖一个加载中的loading;等操作完成,切换到UI线程时,我们再把它隐藏起来.

于是有

private final HomeContract.View mView;
retrofit.create(ApiService.class)
.getHomeData(loginCallInfo.getAccessToken(), loginCallInfo.getUserId(), Constants.PLATFORM, Constants.APPLICATIONID, 1)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<HomeData>() {
@Override
public void onSubscribe(Disposable d) {
if (!d.isDisposed()) {
mView.setLoadingView();
}
} @Override
public void onNext(HomeData value) {
if (value == null) {
mView.setEmptyView();
} else {
mView.setSuccessView();
...//这里显示正常视图
}
} @Override
public void onError(Throwable t) {
Logger.d(t.getMessage());
mView.setErrorView();
} @Override
public void onComplete() { }
});

在HomeContract.java中

public class HomeContract {

    interface View extends BaseView<Presenter> {

        void setLoadingView();

        void setSuccessView();

        void setErrorView();

        void setEmptyView();
}
}

具体在HomeFragment里的实现

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
savedInstanceState) {
  View root =inflater.inflate(R.layout.fragment_home,container,false);
  progress = (ProgressBar) root.findViewById(R.id.loading);
  fl_excep = (FrameLayout) root.findViewById(R.id.fl_error);
  retry = ((Button) root.findViewById(R.id.btn_retry));
  retry.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Logger.e("正在重试");//重试操作
     }
  });
} @Override
public void setLoadingView() {
if (progress.getVisibility() == View.GONE) {
progress.setVisibility(View.VISIBLE);
}
} @Override
public void setSuccessView() {
if (progress.getVisibility() == View.VISIBLE) {
progress.setVisibility(View.GONE);
}
} @Override
public void setErrorView() {
if (progress.getVisibility() == View.VISIBLE) {
progress.setVisibility(View.GONE);
fl_excep.setVisibility(View.VISIBLE);
retry.setText(R.string.request_err);
}
} @Override
public void setEmptyView() {
if (progress.getVisibility() == View.VISIBLE) {
progress.setVisibility(View.GONE);
fl_excep.setVisibility(View.VISIBLE);
retry.setText(R.string.empty_des);
}
}

最后,看下xml布局

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/translucent">
<--这里是你想显示的正常视图-->
<include layout="@layout/public_loading" />
</FrameLayout>

public_loading.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" /> <FrameLayout
android:id="@+id/fl_error"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"> <Button
android:id="@+id/btn_retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@null"
android:text="网络错误,点击重试"
android:textSize="20sp" />
</FrameLayout>
</FrameLayout>
这样,一个控制加载状态的功能就写完了.

RxJava异步请求加载状态控制的更多相关文章

  1. jQuery.ajax( options ) : 通过 HTTP 请求加载远程数据

    jQuery.ajax( options ) : 通过 HTTP 请求加载远程数据 这个是jQuery 的底层 AJAX 实现.简单易用的高层实现见 $.get, $.post 等. $.ajax() ...

  2. Android高效异步图片加载框架

    概述 Android高效异步图片加载框架:一个高效的异步加载显示的图片加载框架,同时具备图片压缩,缓存机制等特性. 详细 代码下载:http://www.demodashi.com/demo/1214 ...

  3. Echarts 异步数据加载遇到的问题

    看了Echarts官网异步加载数据的Demo var myChart = echarts.init(document.getElementById('main')); // 显示标题,图例和空的坐标轴 ...

  4. js 判断页面加载状态

    //----判断当前页面是否加载状态 开始 ---- document.onreadystatechange = subSomething;//当页面加载状态改变的时候执行这个方法. function ...

  5. echarts异步数据加载(在下拉框选择事件中异步更新数据)

    接触echarts 大半年了,从不会到熟练也做过不少的图表,隔了一段时间没使用这玩意,好多东西真心容易忘了.在接触echarts这期间也没有总结什么东西,今天我就来总结一下如何在echart中异步加载 ...

  6. OC 异步顺序加载的方法

    方法一:发射信号量 OC -异步顺序加载 先调用A接口,再调用B接口,再调用C接口 dispatch_semaphore_t semaphore = dispatch_semaphore_create ...

  7. 三、js提交请求加载启动动画、请求完成成功回调、注销加载动画

    1.通过Query  post方式进行异步请求方法 jQuery.post(url, [data], [callback], [type]) 参数说明: url:发送请求地址 data:待发送 Key ...

  8. flask+sqlite3+echarts3+ajax 异步数据加载

    结构: /www | |-- /static |....|-- jquery-3.1.1.js |....|-- echarts.js(echarts3是单文件!!) | |-- /templates ...

  9. javascript 异步模块加载 简易实现

    在javascript是没有类似java或其他语言的模块概念的,因此也不可能通过import或using等关键字来引用模块,这样造成了复杂项目中前端代码混乱,变量互相影响等. 因此在复杂项目中引入AM ...

随机推荐

  1. hihoCoder #1870 : Jin Yong’s Wukong Ranking List-闭包传递(递归) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction A) 2018 ICPC 北京区域赛现场赛A

    P1 : Jin Yong’s Wukong Ranking List Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Desc ...

  2. 【SQL】ORACLE生成临时表

    在日常的SQL查询中,我们需要对要查询的数据进行事先处理,然后再在预先处理好的数据里面进行查询.此时我们就需要用到临时表了,将数据预先处理好放到临时表里面,然后再在临时表里根据我们需要的条件进行查询. ...

  3. 洛谷——P1916 小书童——蚂蚁大战

    P1916 小书童——蚂蚁大战 题目背景 小A在你的帮助下,开始“刷题”,他在小书童里发现了一款叫“蚂蚁大战”(又称蛋糕保卫战)的游戏.(你懂得) 题目描述 游戏中会出现n只蚂蚁,分别有a1,a2…… ...

  4. Xamarin.Forms教程下载安装Windows版的Xamarin开发工具

    Xamarin.Forms教程下载安装Windows版的Xamarin开发工具 下载安装Windows版的Xamarin开发工具 本节将讲解如何下载并安装Windows版的Xamarin开发工具. 下 ...

  5. 安卓 内存泄漏 MemoryAnalyzer

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 需要 获取 root 权限 步骤: 1,使用eclipse 自带的 DDMS 工具分析各线程的内 ...

  6. Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法

    F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...

  7. while an existing transition or presentation is occurring; the navigation stack will not be updated

    使用UIAlertController提示信息,在之后使用navigation进行逻辑跳转时,出现popToViewController:transition: called on <UINav ...

  8. 为什么fis没有freemarker的解决方案啊?_前端吧_百度贴吧

    为什么fis没有freemarker的解决方案啊?_前端吧_百度贴吧 fis-plus:适用于PHP+Smarty后端选型jello:适用于Java+Velocity后端选型goiz:适用于go+ma ...

  9. 这些年,我们一直追随的.NET

    闲来无事,浏览自己的QQ空间,意外发现自己在13年1月份的发在QQ空间写的一片关于技术的随笔,觉得应该将其移到这里: 这些年,我们一直追随的.NET        前两天,意外地看到了.NET平台为异 ...

  10. mongo 实时同步工具 mongosync

    文档地址:https://github.com/Qihoo360/mongosync/wiki/%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B #数据全量备份mongodum ...