Last active
January 19, 2016 22:20
-
-
Save hanspeide/1bd8fe5d78b0131620f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.example; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
public abstract class EndlessRecyclerViewOnScrollListener extends RecyclerView.OnScrollListener { | |
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 = 0; | |
private LinearLayoutManager mLinearLayoutManager; | |
public EndlessRecyclerViewOnScrollListener(LinearLayoutManager linearLayoutManager) { | |
this.mLinearLayoutManager = linearLayoutManager; | |
} | |
@Override | |
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
visibleItemCount = recyclerView.getChildCount(); | |
totalItemCount = mLinearLayoutManager.getItemCount(); | |
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition(); | |
if (loading) { | |
if (totalItemCount > previousTotal) { | |
loading = false; | |
previousTotal = totalItemCount; | |
} | |
} | |
if (!loading && (totalItemCount - visibleItemCount) | |
<= (firstVisibleItem + visibleThreshold)) { | |
onLoadMore(++currentPage); | |
loading = true; | |
} | |
} | |
public abstract void onLoadMore(int current_page); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment