Android recyclerview endless scroll listner onScrolled() getting called twice - android

This is how I am setting up recyclerview.
mRecyclerViewRides = (RecyclerView) findViewById(R.id.recyclerViewRides);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerViewRides.setHasFixedSize(true);
// use a linear layout manager
mLinearLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerViewRides.setLayoutManager(mLinearLayoutManager);
mRideListAdapter = new FindRideListAdapter(getApplicationContext(), mRecyclerViewRides, mRideDetailArrayList, mImageOptions, mImageLoader, this);
mRecyclerViewRides.setAdapter(mRideListAdapter);
mRecyclerViewRides.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLinearLayoutManager) {
#Override
public void onLoadMore(int current_page) {
Log.e(TAG,"Scroll Count ==> "+(++mIntScrollCount));
if(!flagFirstLoad) {
mOffset = mOffset + mLimit;
getRideList(mOffset, mLimit);
}
}
});
This is the code for endless recycler view scroll listner.
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 = 1; // 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;
}
/**
* function to reset values of the properties of this class to initial
*/
public void resetValues(){
previousTotal = 0;
loading = true;
visibleThreshold = 1;
firstVisibleItem = 0;
visibleItemCount = 0;
totalItemCount = 0;
}
#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);}
So now whenever i scroll to bottom of the list onLoadMore() gets called twice and even intialy when i call getRideList() for the first time onScroll is getting invoked. I don't understand why?

Updated my support library and its working fine now.

Related

Android RecyclerView Pagination not working if recyclerview does not take full screen height

I have a recyclerview with a bottom tab bar below it I have tried to achieve this using lots of code snippets but the event just wont get called
This is how i am trying it, first i created a Listener and then used it in my fragment.
public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerViewScrollListener.class
.getSimpleName();
private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 1;
private LinearLayoutManager mLinearLayoutManager;
public EndlessRecyclerViewScrollListener(
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);
}
this is th code i write in my fragment
rv_influencer.addOnScrollListener(new EndlessRecyclerViewScrollListener((linearLayoutManager)) {
#Override
public void onLoadMore(int current_page) {
page_no++;
getInfluencerData();
Toast.makeText(getActivity(), "Scroll", Toast.LENGTH_LONG).show();
}
});

onLoadMoreListener won't stop loading - RecyclerView - Android

I have a user feed where user's posts will be displayed if the current user is following them, at the moment I have the loadMoreListener working fine in regards to it loading the next batch of images when they get to the bottom of the RecyclerView. But, when I reach the last item (the feeds only shows 50 posts) in the List it'll attempt to load more and cause an OutOfMemoryException. I've restricted the size of the list in the adapter, but at the minute I can't seem to work out when the user has hit the very bottom to stop the progress from displaying and to stop the OnLoadMoreListener from triggering. This is what I have tried so far:
Adapter Constructor:
public RecyclerViewAdapterAllFeeds(Context context,
final ArrayList<AllFeedsDataModel> previousPostsList, final boolean profile, RecyclerView recyclerView, final ProgressBar progressBar) {
this.context = context;
this.previousPostsList = previousPostsList;
this.profile = profile;
if(recyclerView.getLayoutManager() instanceof LinearLayoutManager){
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager)recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
progressBar.setVisibility(View.VISIBLE);
if (getItemCount() > LOADED_POSTS - 2) {
totalItemCount = getItemCount();
// total_posts = getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (lastVisibleItem <= TOTAL_POSTS) {
if (!loading && totalItemCount <= (lastVisibleItem)) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
}
}
});
}
}
onLoadMoreListener
adapter = new RecyclerViewAdapterAllFeeds(getActivity(), latestUpdatesList, false, recyclerView, progressBar);
recyclerView.setAdapter(adapter);// set adapter on recyclerview
adapter.setOnLoadMoreListener(new RecyclerViewAdapterAllFeeds.OnLoadMoreListener() {
#Override
public void onLoadMore() {
refreshCount++;
populateRecyclerView(true, refreshCount);
adapter.update(updatesList);
adapter.setLoaded();
System.out.println("load");
}
});
adapter.notifyDataSetChanged();// Notify the adapter
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.INVISIBLE);
My question is how can I stop it loading if the total posts is less than that of the restrictions or to know when the final item is visible?
First create EndlessRecyclerViewScrollListener.java:
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 EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
RecyclerView.LayoutManager mLayoutManager;
public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
}
public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
public EndlessRecyclerViewScrollListener(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 LinearLayoutManager) {
lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
} else if (mLayoutManager instanceof GridLayoutManager) {
lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
}
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) {
this.loading = true;
}
}
// If it’s still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
}
// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
// threshold should reflect how many total columns there are too
if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
currentPage++;
onLoadMore(currentPage, totalItemCount);
loading = true;
}
}
// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount);
}
And then implement in this way:
recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(recyclerView.getLayoutManager()) {
#Override
public void onLoadMore(int page, int totalItemsCount) {
//TODO Add logic for load more posts or show message if posts is ended.
}
});
Check out the code which I use for endless scroll:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
// if (BuildConfig.DEBUG)
// Log.d(TAG, "onScrolled: [x, y]=[" + dx + ", " + dy + "], count=[" + recyclerView.getLayoutManager().getChildCount() + ", " + recyclerView.getLayoutManager().getItemCount() + ", " + ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition() + "]");
if (hasMoreRequest) {
if (dy > 0) { //check for scroll down
checkForMoreLoad();
}
}
}
});
private void checkForMoreLoad() {
final Handler handler = new Handler();
Runnable checkForMoreData = new Runnable() {
#Override
public void run() {
if (null != recyclerView) {
int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
int totalItemCount = recyclerView.getLayoutManager().getItemCount();
int pastVisibleItems = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
onScrollToLoad(visibleItemCount, pastVisibleItems, totalItemCount);
}
}
};
handler.postDelayed(checkForMoreData, 100);
}
private void onScrollToLoad(int visibleItemCount, int pastVisibleItems, int totalItemCount) {
if ((visibleItemCount + pastVisibleItems) + 4 >= totalItemCount && hasMoreRequest) {
if (BuildConfig.DEBUG)
Log.d(TAG, "onScroll lastInScreen - so load more");
startNetworkCall(page + 1);
}
}
And after data from API, if there is more data after that I will again call checkForMoreLoad().
The line which has (visibleItemCount + pastVisibleItems) + 4 in onScrollToLoad() has 4 in it. It is for starting API before hand of reaching end of the scroll.

Endless scroll listner android reset the page no from activity

I have implemented pagination using EndlessRecyclerOnScrollListener. At a particular scenario I have to reset the page number, so i have created resetValues() method. But I don't know how to call this method from activity class.
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 = 0; // 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,boolean isfromversion) {
this.mLinearLayoutManager = linearLayoutManager;
Log.e("isversionendless",isfromversion+"");
if(isfromversion)
{
resetValues();
}
}
#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 void resetValues(){
previousTotal = 0;
loading = true;
visibleThreshold = 1;
firstVisibleItem = 0;
visibleItemCount = 0;
totalItemCount = 0;
}
public abstract void onLoadMore(int current_page);
}
EndlessRecyclerOnScrollListener scrollListener=new EndlessRecyclerOnScrollListener(
mGridLayoutManager) {
#Override
public void onLoadMore(int current_page) {
// do somthing...
loadMoreData(current_page);
}
};
recyclerView.setOnScrollListener(scrollListener);
when you want to reset values then just simply call method as below.
scrollListener.resetValues();

Detect RecyclerView's GridLayoutManager End Of Scroll with SuperSLiM library

I have been searching for a while but can't find a solution to detect end of scroll of recycler view with grid layout manager. Using code below actually work, but it is for linear layout manager not for grid layout manager.
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
final int treeshold = 0;
try {
if (scrollState == RecyclerView.SCROLL_STATE_IDLE) {
if (((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition() >= yourData.size()
- 1 - treeshold) {
//your load more logic
}
}
} catch (Exception e) {
}
}
The idea is that i want to implement load more function to my application, so i need to detect end of scroll.
Edit : maybe the problem not the grid view itself. I used com.tonicartos.superslim library to get sticky header view. I wonder that it might be the problem
Add a scroll listener to your RecyclerView like below:
int pastVisiblesItems, visibleItemCount, totalItemCount;
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
//bottom of recyclerview
}
}
});
layoutmanager is your GridLayoutManager
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);
I just sent an email directly to Mr. Artos about this problem and got a reply:
"It likely won't work properly until the next version is finished. I wrote about it on my blog at http://www.tonicartos.com/2015/12/superslim-milestone-1_23.html?m=1"

Android - Endless scrolling with SuperSLiM

There is any way to implement endless scrolling to load more itens when i use SuperSLiM library?
Normally I'm used to use the LinearLayoutManager.findFirstVisibleItemPosition() method to help me to know when I need load more itens... but now, with linearlayout of SuperSLiM i can't use this.
How can i implement this feature?
In place of LinearLayoutManager,use LayoutManager of superslim. I had implemented it in one of demo project. Row code for that is,
public abstract class EndlessRecyclerOnScrollListener 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 current_page = 1;
private LayoutManager mlayoutManager;
public EndlessRecyclerOnScrollListener(LayoutManager linearLayoutManager) {
this.mlayoutManager = layoutManager;
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mlayoutManager.getItemCount();
firstVisibleItem = mlayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something here
current_page++;
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}
I hope this will help you. :)

Categories

Resources