I'm just starting using RxJava and I'm struggling to figure out how to use observables the right way.
Down below I have a pager and I'd like to call a web service on page scroll avoiding to many useless calls.
So I found that 'debounce' operator is what I am looking for but in my case it's not working and the web service is called everytime..
v.pager.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
val itemPosition : Int = layoutManager.findFirstCompletelyVisibleItemPosition();
Observable.just(itemPosition).debounce(1500, TimeUnit.MILLISECONDS).map {
retrieveUserDetail(userList[itemPosition])
}.observeOn(AndroidSchedulers.mainThread()).subscribe()
}
}
})
What's wrong with this code?