Created
January 11, 2019 10:18
-
-
Save hitesh-dhamshaniya/e9c4f893d7224d39ebc7f2bb3f43d446 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
import androidx.recyclerview.widget.GridLayoutManager; | |
import androidx.recyclerview.widget.LinearLayoutManager; | |
import androidx.recyclerview.widget.RecyclerView; | |
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener { | |
public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName(); | |
private int previousTotal = 0; // The total number of items in the dataset after the last load | |
private boolean loading = true; // True if we are still waiting for the last set of data to load. | |
private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more. | |
int firstVisibleItem, visibleItemCount, totalItemCount; | |
private int currentPage = 1; | |
private RecyclerView.LayoutManager mLayoutManager; | |
private boolean isUseLinearLayoutManager; | |
private boolean isUseGridLayoutManager; | |
public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) { | |
this.mLayoutManager = linearLayoutManager; | |
isUseLinearLayoutManager = true; | |
} | |
public EndlessRecyclerOnScrollListener(GridLayoutManager gridLayoutManager) { | |
this.mLayoutManager = gridLayoutManager; | |
isUseGridLayoutManager = true; | |
} | |
@Override | |
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
visibleItemCount = recyclerView.getChildCount(); | |
totalItemCount = mLayoutManager.getItemCount(); | |
if (isUseLinearLayoutManager && mLayoutManager instanceof LinearLayoutManager) { | |
firstVisibleItem = ((LinearLayoutManager) mLayoutManager).findFirstVisibleItemPosition(); | |
} | |
if (isUseGridLayoutManager && mLayoutManager instanceof GridLayoutManager) { | |
firstVisibleItem = ((GridLayoutManager) mLayoutManager).findFirstVisibleItemPosition(); | |
} | |
if (loading) { | |
if (totalItemCount > previousTotal) { | |
loading = false; | |
previousTotal = totalItemCount; | |
} | |
} | |
if (!loading && (totalItemCount - visibleItemCount) | |
<= (firstVisibleItem + visibleThreshold)) { | |
// End has been reached | |
// Do something | |
currentPage++; | |
onLoadMore(currentPage); | |
loading = true; | |
} | |
} | |
public abstract void onLoadMore(int currentPage); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment