假设我们获得了分页API Single<List<Foo>> getFoos(pageToken: String?)
并使用RxBinding来观看RecyclerView滚动事件RxRecyclerView.scrollEvents(recyclerView)
。
据我所知,实现一个将scroll事件转换为pageToken的运算符。这样,可流动的滚动事件取决于API的结果。我应该以递归的方式实现它还是有任何优雅的方式?
答案 0 :(得分:0)
如果要对rxJava使用分页,则无需使用rxBinding 你可以自己做rxjava分页:
private Observable<Integer> getScrollObservable(RecyclerView recyclerView, int limit, int emptyListCount) {
return Observable.create(subscriber -> {
final RecyclerView.OnScrollListener sl = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (!subscriber.isUnsubscribed()) {
int position = getLastVisibleItemPosition(recyclerView);
int updatePosition = recyclerView.getAdapter().getItemCount() - 1 - (limit / 2);
if (position >= updatePosition) {
int offset = emptyListCountPlusToOffset ? recyclerView.getAdapter().getItemCount() : recyclerView.getAdapter().getItemCount() - emptyListCount;
subscriber.onNext(offset);
}
}
}
};
recyclerView.addOnScrollListener(sl);
subscriber.add(Subscriptions.create(() -> recyclerView.removeOnScrollListener(sl)));
if (recyclerView.getAdapter().getItemCount() == emptyListCount) {
int offset = emptyListCountPlusToOffset ? recyclerView.getAdapter().getItemCount() : recyclerView.getAdapter().getItemCount() - emptyListCount;
subscriber.onNext(offset);
}
});
}
查看完整example