By rewriting the Onscrolllistener to monitor whether the recyclerview slides to the bottom

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.