Reputation: 41
I want to set a count down timer inside each event in recyclerview.
I tried it inside onBindViewHolder. It is working but at the same time it is also affecting the UI.
For example, click events are not working.
Is the any best approach to resolve it?
Upvotes: 1
Views: 88
Reputation: 722
You could use Observable.interval
from RxJava
Here's a util function for countdowntimer
public static Observable<Long> countdownTimer(long countdownValue, Observer<Long> observer) {
if (observer == null) return Observable.just((long) 0);
//.interval is a cold observable
// it will emit separately for every observer
Observable<Long> timerObservale = Observable
.interval(1, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.takeWhile(new Predicate<Long>() {
@Override
public boolean test(Long aLong) throws Exception {
return aLong <= countdownValue;
}
})
.doOnError(Throwable::printStackTrace)
.observeOn(AndroidSchedulers.mainThread());
timerObservale.subscribe(observer);
return timerObservale;
}
Here's how you would use it
Utils.countdownTimer(60, new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Long aLong) {
//do somethng
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onComplete() {
}
});
The utils function emits a long every second for a given period of time
Upvotes: 2