Created
September 14, 2016 15:10
-
-
Save MrVilkaman/5e9455169274069e74b4e208c20e7f99 to your computer and use it in GitHub Desktop.
Rx pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void bindListener() { | |
Observable<Integer> scrollObs = view().getScrollObs(LIMIT); | |
subscribe = scrollObs.subscribe(new MyTripsViewListViewSubscriber(view())); | |
} | |
private void clearObs() { | |
if (subscribe != null && !subscribe.isUnsubscribed()) { | |
subscribe.unsubscribe(); | |
} | |
} | |
public void refresh() { | |
clearObs(); | |
tripsUseCase.setOffset(0,LIMIT).execute(new MyLoadSubscriber(view(),true)); | |
} | |
private class MyTripsViewListViewSubscriber extends ViewSubscriber<MyTripsView, Integer> { | |
public MyTripsViewListViewSubscriber(MyTripsView view) { | |
super(view); | |
} | |
@Override | |
public void onNext(Integer integer) { | |
// Загрузка новой порции данных | |
tripsUseCase.setOffset(integer,LIMIT).execute(new MyLoadSubscriber(view(),false)); | |
} | |
} | |
private class MyLoadSubscriber extends LoadSubscriber<MyTripsView, List<TripsModel>> { | |
private final boolean isNew; | |
public MyLoadSubscriber(MyTripsView view, boolean isNew) { | |
super(view); | |
this.isNew = isNew; | |
} | |
@Override | |
public void onNext(List<TripsModel> item) { | |
view().bind(item,isNew); | |
if (isNew) { | |
bindListener(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public Observable<Integer> getScrollObs(int limit) { | |
int itemCount = adapter.getItemCount(); | |
return ViewUtils.getScrollObservable(recyclerView, limit, itemCount < limit ? itemCount : 0); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ViewUtils { | |
public static Observable<Integer> getScrollObservable(RecyclerView recyclerView, int limit, int emptyListCount) { | |
Observable<Integer> integerObservable = 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) { | |
subscriber.onNext(recyclerView.getAdapter() | |
.getItemCount()); | |
} | |
} | |
} | |
}; | |
recyclerView.addOnScrollListener(sl); | |
subscriber.add(Subscriptions.create(() -> recyclerView.removeOnScrollListener(sl))); | |
if (recyclerView.getAdapter() | |
.getItemCount() == emptyListCount) { | |
subscriber.onNext(recyclerView.getAdapter() | |
.getItemCount()); | |
} | |
}); | |
return integerObservable.distinctUntilChanged(); | |
} | |
private static int getLastVisibleItemPosition(RecyclerView recyclerView) { | |
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); | |
Class recyclerViewLMClass = layoutManager.getClass(); | |
if (recyclerViewLMClass == LinearLayoutManager.class || LinearLayoutManager.class.isAssignableFrom(recyclerViewLMClass)) { | |
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; | |
return linearLayoutManager.findLastVisibleItemPosition(); | |
} else if (recyclerViewLMClass == StaggeredGridLayoutManager.class || StaggeredGridLayoutManager.class.isAssignableFrom(recyclerViewLMClass)) { | |
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; | |
int[] into = staggeredGridLayoutManager.findLastVisibleItemPositions(null); | |
List<Integer> intoList = new ArrayList<>(); | |
for (int i : into) { | |
intoList.add(i); | |
} | |
return Collections.max(intoList); | |
} | |
throw Exceptions.propagate(new Throwable("Unknown LayoutManager class: " + recyclerViewLMClass.toString())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment