rxjava2 延时

 kotlin 代码  

 mTimeDisposable = Observable.timer(3000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object:Consumer<Long>{
                    override fun accept(t: Long) {
                        //TODO("Not yet implemented")
                        iv_welcome.visibility = View.VISIBLE
                        ObjectAnimator.ofFloat(iv_welcome, "alpha", 0F, 1F).setDuration(500).start()
                    }
                }
                )

//下面代码功能和上面一样时省略版

      /* mTimeDisposable = Observable.timer(3000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    iv_welcome.visibility = View.VISIBLE
                    ObjectAnimator.ofFloat(iv_welcome, "alpha", 0F, 1F).setDuration(500).start()
                })*/

 

RxJava2简单使用二(Consumer)

java代码  

        /**
         * Consumer是简易版的Observer,他有多重重载,可以自定义你需要处理的信息,我这里调用的是只接受onNext消息的方法,
         * 他只提供一个回调接口accept,由于没有onError和onCompete,无法再 接受到onError或者onCompete之后,实现函数回调。
         * 无法回调,并不代表不接收,他还是会接收到onCompete和onError之后做出默认操作,也就是监听者(Consumer)不在接收
         * Observable发送的消息,下方的代码测试了该效果。
         */
        final Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(@NonNull String s) throws Exception {
                Log.d("MainActivity", Thread.currentThread().getName() + " String:" + s);
            }
        };
 
        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
                Log.d("MainActivity", Thread.currentThread().getName() + "emit Hello");
                e.onNext("Hello");
                Log.d("MainActivity", Thread.currentThread().getName() + "emit Complete");
                e.onComplete();
                Log.d("MainActivity", Thread.currentThread().getName() + "emit World");
                e.onNext("World");
            }
        });

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值