Last active
November 8, 2019 06:27
-
-
Save skyfishjy/c6d016943de94a6792dd to your computer and use it in GitHub Desktop.
EndlessRecyclerOnScrollListener
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.skyfishjy.kkmh.ui.adapter; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
/** | |
* Created by skyfishjy on 12/30/14. | |
*/ | |
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener { | |
int firstVisibleItem, visibleItemCount, totalItemCount; | |
private int previousTotal = 0; | |
private boolean loading = true; | |
private int visibleThreshold = 5; | |
private int current_page = 1; | |
private LinearLayoutManager mLinearLayoutManager; | |
public EndlessRecyclerOnScrollListener(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)) { | |
// End has been reached | |
// Do something | |
current_page++; | |
onLoadMore(current_page); | |
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