Skip to content

Instantly share code, notes, and snippets.

@tsuginodan
Created June 5, 2016 02:27
Show Gist options
  • Save tsuginodan/82b16f4b7ae4dfa81ff6b71f6c188227 to your computer and use it in GitHub Desktop.
Save tsuginodan/82b16f4b7ae4dfa81ff6b71f6c188227 to your computer and use it in GitHub Desktop.
(WIP) RecyclerView GridLayout InfiniteScroll
int gridColumns = 4;
int visibleThreshold = 3;
int limit = 40
myObject apiResourceList = Constants.API_URL + "pokemon/?limit=" + limit;
.
.
.
RecyclerView.OnScrollListener paginationScrollListener = new RecyclerView.OnScrollListener() {
int gridSpan, pastVisibleItems, lastVisibleItem, visibleItemCount, totalItemCount;
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//works well with linearlayoutmanager
if(dy > 0){//scroll down
//but for a gridlayout because the spancount
//gridSpan = gridLayoutManager.getSpanCount();
visibleItemCount = gridLayoutManager.getChildCount();//current # of childs
totalItemCount = gridLayoutManager.getItemCount();//# of items
pastVisibleItems = gridLayoutManager.findFirstVisibleItemPosition();
lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
if (apiResourceList.getNext() != null) {
//visibleItemCount + pastVisibleItems
if ((lastVisibleItem + visibleThreshold) >= totalItemCount) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mPresenter.onLoadList(apiResourceList.getNext());//url
}
},2000);//wait two seconds
}
}
}
}
};
@Override
public void onLoadList(final String url) {
Observable<ApiResourceList> observable = mModel.getPkmnListPagination(url)
.observeOn(AndroidSchedulers.mainThread())
//map?? i need the result array of response but at the same time "ApiResourceList" object
.debounce(400L, TimeUnit.MILLISECONDS);//because the rv scroll listener
subscription = observable.subscribe(new Observer<ApiResourceList>() {
@Override
public void onCompleted() {
Log.e("TeamBuildPresenter", "First observable complete " + url);
mView.hideProgress();
}
@Override
public void onError(Throwable e) {
mView.showListPlaceholder();
mView.hideProgress();
}
@Override
public void onNext(ApiResourceList result) {
mView.bindDataList(result);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment