Android Rxjava 项目实战

本文介绍了在Android项目中使用RxJava的实战步骤,包括添加依赖、创建RxLifecycleUtils辅助类,以及如何在BaseActivity、BaseFragment和BasePresenter中集成。通过这种方式,可以将耗时操作移到非UI线程,避免阻塞主线程,确保应用流畅运行。
摘要由CSDN通过智能技术生成

 使用步骤;

  第一步; 添加依赖

  • api 'io.reactivex.rxjava2:rxandroid:2.1.0'

  • api 'io.reactivex.rxjava2:rxjava:2.1.16'

  • api 'com.uber.autodispose:autodispose:0.7.0'

  • api 'com.uber.autodispose:autodispose-android-archcomponents:0.7.0'

第二步:

 添加类  RxLifecycleUtils.java  代码如下:

public class RxLifecycleUtils {
    private RxLifecycleUtils() {
        throw new IllegalStateException("Can't instance the RxLifecycleUtils");
    }


    public static <T> AutoDisposeConverter<T> bindLifecycle(LifecycleOwner lifecycleOwner) {
        return AutoDispose.autoDisposable(
                AndroidLifecycleScopeProvider.from(lifecycleOwner.getLifecycle(), Lifecycle.Event.ON_DESTROY)
        );
    }

 3.在BaseActivity  和BaseFragment中添加

 public LifecycleOwner getLifecycleOwner(){
    return this;
}

3.1 在BasePresenter中添加

protected <T> AutoDisposeConverter<T> bindLifecycle() {
    if (null == getLifecycleOwner())
        throw new NullPointerException("lifecycleOwner == null");
    return RxLifecycleUtils.bindLifecycle(getLifecycleOwner());
}


public LifecycleOwner getLifecycleOwner() {
    return getV().getLifecycleOwner();
}

4.  使用案例; 

public class Tes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值