为了使用RxJava实现在ImageView中每隔指定时间加载一张系统图片的效果,
Observable.from(getUri()).timer(2,TimeUnit.SECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(uri
-> {
Log.e("uri",uri+"");
Glide.with(this).load(uri).into(img);});
}
在getUri()中返回了一个ArrayList<Uri>对象,然后每次发射一个uri到订阅者中,怎么让这个发射过程延迟调用。
使用timer操作符出现了以下错误:Unknown type class java.lang.Long. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class
This error seems to be caused by your Glide and has nothing to do with Timer!
timer is a delayed emission function, which is only executed once. If you want to send data regularly, try the interval function:
For scheduled tasks, you must start from the interval. Don’t always think about starting from the data
The author’s mistake is that timer is a static method, and the previous from(getUri()) has no effect at all. What is sent is not the image address, but the number of cycles. The type is Long. After throwing it to Glide, the type is wrong.
Just follow a delay after starting from. The specific API is as follows http://reactivex.io/RxJava/javadoc/rx/Observable.html#delay(rx.functions.Func1)