onLoadMoreListener won't stop loading - RecyclerView - Android -
i have user feed user's posts displayed if current user following them, @ moment have loadmorelistener
working fine in regards loading next batch of images when bottom of recyclerview
. but, when reach last item (the feeds shows 50 posts) in list it'll attempt load more , cause outofmemoryexception
. i've restricted size of list in adapter, @ minute can't seem work out when user has hit bottom stop progress displaying , stop onloadmorelistener triggering. have tried 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 adapter progressbar.setindeterminate(false); progressbar.setvisibility(view.invisible);
my question how can stop loading if total posts less of restrictions or know when final item 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 { // minimum amount of items have below current scroll position // before loading more. private int visiblethreshold = 5; // current offset index of data have loaded private int currentpage = 0; // total number of items in dataset after last load private int previoustotalitemcount = 0; // true if still waiting last set of data load. private boolean loading = true; // sets 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; (int = 0; < lastvisibleitempositions.length; i++) { if (i == 0) { maxsize = lastvisibleitempositions[i]; } else if (lastvisibleitempositions[i] > maxsize) { maxsize = lastvisibleitempositions[i]; } } return maxsize; } // happens many times second during scroll, wary of code place here. // given few useful parameters work out if need load more data, // first check if waiting previous load 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); // maximum element within list lastvisibleitemposition = getlastvisibleitem(lastvisibleitempositions); } else if (mlayoutmanager instanceof linearlayoutmanager) { lastvisibleitemposition = ((linearlayoutmanager) mlayoutmanager).findlastvisibleitemposition(); } else if (mlayoutmanager instanceof gridlayoutmanager) { lastvisibleitemposition = ((gridlayoutmanager) mlayoutmanager).findlastvisibleitemposition(); } // if total item count 0 , previous isn't, assume // list invalidated , should reset initial state if (totalitemcount < previoustotalitemcount) { this.currentpage = this.startingpageindex; this.previoustotalitemcount = totalitemcount; if (totalitemcount == 0) { this.loading = true; } } // if it’s still loading, check see if dataset count has // changed, if conclude has finished loading , update current page // number , total item count. if (loading && (totalitemcount > previoustotalitemcount)) { loading = false; previoustotalitemcount = totalitemcount; } // if isn’t loading, check see if have breached // visiblethreshold , need reload more data. // if need reload more data, execute onloadmore fetch data. // threshold should reflect how many total columns there if (!loading && (lastvisibleitemposition + visiblethreshold) > totalitemcount) { currentpage++; onloadmore(currentpage, totalitemcount); loading = true; } } // defines process loading more data based on page public abstract void onloadmore(int page, int totalitemscount); }
and implement in way:
recyclerview.addonscrolllistener(new endlessrecyclerviewscrolllistener(recyclerview.getlayoutmanager()) { @override public void onloadmore(int page, int totalitemscount) { //todo add logic load more posts or show message if posts ended. } });
Comments
Post a Comment