To increase reusability and flexibility, we still define an interface to listen to the callback at the bottom of the scroll, so you can use it in Listview,scrollview.
Onbottomlistener
Package Kale.com.waterfall; /** @author*/Publicinterface Onbottomlistener { publicvoid onbottom ();}
Then, let's rewrite Recyclerview.onscrolllistener.
PackageKale.com.waterfall.extra.RecyclerView;ImportAndroid.support.v7.widget.GridLayoutManager;ImportAndroid.support.v7.widget.LinearLayoutManager;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.support.v7.widget.StaggeredGridLayoutManager;ImportKale.com.waterfall.OnBottomListener;/** * @authorJack Tony * @brief recyle View scrolling listener * @date 2015/4/6*/ Public classOnrcvscrolllistenerextendsRecyclerview.onscrolllistenerImplementsOnbottomlistener {PrivateString TAG =getclass (). Getsimplename (); Public Static enumLayout_manager_type {LINEAR, GRID, Staggered_grid}/*** Type of LayoutManager (enumeration)*/ protectedLayout_manager_type Layoutmanagertype; /*** The last position*/ Private int[] lastpositions; /*** Position of last visible item*/ Private intlastvisibleitemposition;/* *//*** is loading*//*Private Boolean isloadingmore = false;*/ /*** Status of the current slide*/ Private intCurrentscrollstate = 0; @Override Public voidOnscrolled (Recyclerview Recyclerview,intDxintdy) { Super. onscrolled (recyclerview, DX, dy); Recyclerview.layoutmanager LayoutManager=Recyclerview.getlayoutmanager (); //int lastvisibleitemposition =-1; if(Layoutmanagertype = =NULL) { if(LayoutManagerinstanceofLinearlayoutmanager) {Layoutmanagertype=Layout_manager_type. LINEAR; } Else if(LayoutManagerinstanceofGridlayoutmanager) {Layoutmanagertype=Layout_manager_type. GRID; } Else if(LayoutManagerinstanceofStaggeredgridlayoutmanager) {Layoutmanagertype=Layout_manager_type. Staggered_grid; } Else { Throw NewRuntimeException ("Unsupported LayoutManager used. Valid ones is Linearlayoutmanager, Gridlayoutmanager and Staggeredgridlayoutmanager "); } } Switch(layoutmanagertype) { Caselinear:lastvisibleitemposition=((Linearlayoutmanager) layoutmanager). Findlastvisibleitemposition (); Break; Casegrid:lastvisibleitemposition=((Gridlayoutmanager) layoutmanager). Findlastvisibleitemposition (); Break; CaseStaggered_grid:staggeredgridlayoutmanager Staggeredgridlayoutmanager=(Staggeredgridlayoutmanager) LayoutManager; if(Lastpositions = =NULL) {lastpositions=New int[Staggeredgridlayoutmanager.getspancount ()]; } staggeredgridlayoutmanager.findlastvisibleitempositions (Lastpositions); Lastvisibleitemposition=Findmax (lastpositions); Break; }} @Override Public voidOnscrollstatechanged (Recyclerview Recyclerview,intnewstate) { Super. onscrollstatechanged (Recyclerview, newstate); Currentscrollstate=newstate; Recyclerview.layoutmanager LayoutManager=Recyclerview.getlayoutmanager (); intVisibleItemCount =Layoutmanager.getchildcount (); intTotalitemcount =Layoutmanager.getitemcount (); if((VisibleItemCount > 0 && currentscrollstate = = Recyclerview.scroll_state_idle &&(lastvisibleitemposition)>= totalItemCount-1)) { //log.d (TAG, "is loading more");Onbottom (); }} @Override Public voidOnbottom () {//log.d (TAG, "is Onbottom"); } Private intFindmax (int[] lastpositions) { intmax = Lastpositions[0]; for(intvalue:lastpositions) { if(Value >max) {Max=value; } } returnMax; }}
In the later use, we can listen to the callback method as below:
Waterfallrcv.setonscrolllistener (new Onrcvscrolllistener () { @Override public void Onbottom () { super. Onbottom (); // Automatic loading to the bottom if (! isloadingdata) { "loading old data"); Adapter.loadolddata (); true ; } } });
Listen for Recyclerview by overriding Onscrolllistener to slide to the bottom