Created
April 14, 2018 23:29
-
-
Save eltonjhony/7b8672a561ec3b1265e78d292fb10c2c to your computer and use it in GitHub Desktop.
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
package com.astoria.movieapp; | |
import android.support.v7.widget.GridLayoutManager; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.support.v7.widget.StaggeredGridLayoutManager; | |
public abstract class EndlessScrollListener extends RecyclerView.OnScrollListener { | |
private static final int NUMBER_OF_REMAINING_ITEMS = 1; | |
private int visibleThreshold = 0; | |
private int currentPage = 1; | |
private boolean loading = false; | |
RecyclerView.LayoutManager mLayoutManager; | |
public EndlessScrollListener(LinearLayoutManager layoutManager) { | |
this.mLayoutManager = layoutManager; | |
} | |
public EndlessScrollListener(GridLayoutManager layoutManager) { | |
this.mLayoutManager = layoutManager; | |
visibleThreshold = visibleThreshold * layoutManager.getSpanCount(); | |
} | |
public EndlessScrollListener(StaggeredGridLayoutManager layoutManager) { | |
this.mLayoutManager = layoutManager; | |
visibleThreshold = visibleThreshold * layoutManager.getSpanCount(); | |
} | |
public int getLastVisibleItem(int[] lastVisibleItemPositions) { | |
int maxSize = 0; | |
for (int i = 0; i < lastVisibleItemPositions.length; i++) { | |
if (i == 0) { | |
maxSize = lastVisibleItemPositions[i]; | |
} | |
else if (lastVisibleItemPositions[i] > maxSize) { | |
maxSize = lastVisibleItemPositions[i]; | |
} | |
} | |
return maxSize; | |
} | |
// This happens many times a second during a scroll, so be wary of the code you place here. | |
// We are given a few useful parameters to help us work out if we need to load some more data, | |
// but first we check if we are waiting for the previous load to finish. | |
@Override | |
public void onScrolled(RecyclerView view, int dx, int dy) { | |
int lastVisibleItemPosition = 0; | |
int totalItemCount = mLayoutManager.getItemCount(); | |
if (mLayoutManager instanceof StaggeredGridLayoutManager) { | |
int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null); | |
// get maximum element within the list | |
lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions); | |
} else if (mLayoutManager instanceof GridLayoutManager) { | |
lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition(); | |
} else if (mLayoutManager instanceof LinearLayoutManager) { | |
lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition(); | |
} | |
if (!loading && lastVisibleItemPosition == totalItemCount - NUMBER_OF_REMAINING_ITEMS) { | |
currentPage++; | |
onLoadMore(currentPage, totalItemCount, view); | |
} | |
} | |
public void resetState() { | |
this.currentPage = 1; | |
setLoading(false); | |
} | |
public void setLoading(boolean loading) { | |
this.loading = loading; | |
} | |
// Defines the process for actually loading more data based on page | |
public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment