-
Star
(456)
You must be signed in to star a gist -
Fork
(95)
You must be signed in to fork a gist
-
-
Save ssinss/e06f12ef66c51252563e to your computer and use it in GitHub Desktop.
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.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 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); | |
} |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
public class SampleActivity extends ActionBarActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_sample); | |
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list); | |
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); | |
recyclerView.setLayoutManager(linearLayoutManager); | |
recyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) { | |
@Override | |
public void onLoadMore(int current_page) { | |
// do something... | |
} | |
}); | |
} | |
} |
This thing is all the way bugged.
It stuck when I use SwipeOnRefresh even with proposet reset.
Eventually employed https://gist.github.com/nesquena/d09dc68ff07e845cc622 this
if (layoutManager.findLastCompletelyVisibleItemPosition() == recyclerAdapter.getItemCount() - 1) {
//load more
}
it works good with activity but I am facing problem with fragment onLoadMore not calling.
how can be resolved this ?
How to use it to scroll a "Horizontal" linearLayoutManager ?
Cool. Useful !
How to use it in horizontal Recyclerview ?
Not working when "visibleThreshold" set to 1 & current_page = 0;. Setting "visibleThreshold" as 2 and current_page = 0, cause an unnecessary "onLoadMore" call. Which is result in bad experience to user if there in not data on 2nd page.
It works. Thanks!
It was not working for me because I was wrapping the recycler view in a scroll view in my layout file. I spent days to find that it was the root cause of the issue and that I should remove the scroll view.
Great work!
I'm using kotlin and i was stuck on this one.
Thank
You should add a reset method:
public void reset(int previousTotal, boolean loading) { this.previousTotal = previousTotal; this.loading = loading; }
And call it on onResume() in SampleActivity:
@Override public void onResume() { super.onResume(); endlessScrollListener.reset(0, true); }
Because after you pause the activity and resume again, the condition
!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)
is always false, thusonLoadMore
is never called.
Thanks...Worked like a pro
change
if (totalItemCount > previousTotal)
toif (totalItemCount != previousTotal)
will be better for dataset changing.
Thanks, you saved my time
Thanks you
I have Updated this file as per my usage. I got problem like its calling two times when RecyclerView not have enough (2 or 3 row) data as height of screen.