Android ListView, how to tell if its content has scrolled to the top or bottom?

Source: Internet
Author: User

This method is used to detect:

1Getlistview (). Setonscrolllistener (NewOnscrolllistener () {2 @Override3              Public voidOnscrollstatechanged (Abslistview view,intscrollstate) {4             }5 6 @Override7              Public voidOnscroll (Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) {8                 if(firstvisibleitem==0){9LOG.E ("Log", "Slide to Top");Ten                 } One                 if(visibleitemcount+firstvisibleitem==Totalitemcount) { ALOG.E ("Log", "slip to the bottom"); -                 } -             } the});

from:http://ask.csdn.net/questions/459

This is another kind at the bottom

Listview.setonscrolllistener (NewOnscrolllistener () {@Override Public voidOnscrollstatechanged (Abslistview view,intscrollstate) {      //When you do not scroll     if(Scrollstate = =onscrolllistener.scroll_state_idle) {       //determine if scrolling to the bottom      if(view.getlastvisibleposition () = = View.getcount ()-1) {Adapter.count+ = 10;                    Adapter.notifydatasetchanged (); intCurrentpage=adapter.count/10; Toast.maketext (Getapplicationcontext (),"section" +currentpage+ "page", Toast.length_long). Show (); } }} @Override Public voidOnscroll (Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) {      //TODO auto-generated Method Stub         }    });

The ListView implementation slides to the bottom to load more

Usually when we swipe the page at the bottom of the page, the page will automatically load more data to facilitate our browsing, the more typical example is Twitter.

This design can also be seen on Android, especially when used in ListView . When the ListView slides to the bottom, it displays a progress bar and loads more data.

The method to implement this function is simple, just call ListView the setOnScrollListener() method.

The calling setOnScrollListener() method needs to override two methods, one is a onScrollStateChanged() method to determine ListView the current sliding state, and the other is to onScroll() detect the ListView current view state. Here we just need to use the onScroll() method. See the code and comments below for a detailed procedure.

Listview.setonscrolllistener (NewAbslistview.onscrolllistener () {/** Define a Boolean variable, * to determine if the sliding direction is downward*/    Private BooleanMovetobottom =false; /** Defines an integer variable, * used to record the index value of the first list item visible before the ListView, * mainly used to determine if the sliding direction of the ListView is downward, and the specific method of use is shown below*/    Private intPrevious = 0; @Override Public voidOnscrollstatechanged (Abslistview view,intscrollstate) {        /*You don't have to do anything.*/    }    /** view refers to the current status of the report; * Firstvisibleitem represents the index value of the first list item currently visible in the ListView; * VisibleItemCount indicates the number of list items currently visible in the ListView; * Totalitemcount indicates the total number of list items in the listview * * When the ListView is sliding to the bottom, * totalitemcount = = Firstvisibleitem + visibleite MCount; * However, the ListView does not have to be sliding downward when the above conditions are met, * so we need to add a condition to judge the downward slide, * only the index value of the first list item that is visible before the ListView is required to loop compared previous and the first that is currently visible     The index value of the list item Firstvisibleitem, * When previous < Firstvisibleitem, the list slides down, * When previous > Firstvisibleitem the list slides up, * Remember to assign Firstvisibleitem to previous after the comparison is over, * for next comparison*/@Override Public voidOnscroll (Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) {        if(Previous <Firstvisibleitem) {Movetobottom=true; } Else if(Previous >Firstvisibleitem) {Movetobottom=false; } Previous=Firstvisibleitem; if(Totalitemcount = = Firstvisibleitem + VisibleItemCount &&Movetobottom) {            /*code that needs to load more data*/        }    }});

Bottom automatic loading is useful for paging loads of large amounts of data, but when the amount of data is small, this function becomes cumbersome, such as the total amount of data may also be a screen size, as long as a slight swipe to start loading, but this load is not necessary. The solution to this problem is also very simple, only need to modify the load more judgment conditions, when totalItemCount less than a certain value when the data is not loaded, similarly you can specify the amount of data, when reaching totalItemCount a certain amount of data will not load data.

from:http://mthli.github.io/post/listview%e5%ae%9e%e7%8e%b0%e6%bb%91%e5%8a%a8%e5%88%b0%e5%ba%95%e9%83%a8%e5%8a %a0%e8%bd%bd%e6%9b%b4%e5%a4%9a.html

Android ListView, how to tell if its content has scrolled to the top or 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.