Android Advanced UI Design note 09:android How to implement an infinite scrolling list

Source: Internet
Author: User

The ListView and GridView have become native Android apps implemented in two of the most popular design patterns. At present, these patterns are used by a large number of developers, mainly because they are simple and straightforward implementations, while they provide a good, uncluttered user experience.

A common basic requirement for the ListView and GridView is that when the user scrolls down , the data can be dynamically loaded to support infinite scrolling . Here's how you can implement this feature in your own application.

The specific process is as follows:

(1) One of the main components we need is the Infinitescrolllistener class, which implements the Onscrolllistener interface . Let's look directly at the following code implementations of this class:

Infinitescrolllistener.java

1  Public Abstract classInfinitescrolllistenerImplementsAbslistview.onscrolllistener {2     Private intBufferitemcount = 10;3     Private intcurrentpage = 0;4     Private intItemCount = 0;5     Private BooleanIsLoading =true;6     7      PublicInfinitescrolllistener (intBufferitemcount) {8          This. Bufferitemcount =Bufferitemcount;9     }Ten      One      Public Abstract voidLoadmore (intPageinttotalitemscount); A      - @Override -      Public voidOnscrollstatechanged (Abslistview view,intscrollstate) { the         // do nothing -     } -      - @Override +      Public voidOnscroll (Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) { -         if(Totalitemcount <ItemCount) { +              This. ItemCount =Totalitemcount; A             if(Totalitemcount = = 0) { at                  This. isloading =true;  -             } -         } -          -         if(isloading && (Totalitemcount >ItemCount)) { -IsLoading =false; inItemCount =Totalitemcount; -currentpage++; to         } +          -         if(!isloading && (totalitemcount-visibleitemcount) <= (Firstvisibleitem +Bufferitemcount)) { theLoadmore (currentpage + 1, totalitemcount); *IsLoading =true; $         }Panax Notoginseng     } -}



(2) Add to event into ListView:

Youractivity.java

  //  Attach the listener to the Adapterview onCreate  yourlistview.setonscrolllistener (new  Infinitescrolllistener (5 public
       void  Loadmore (int  page, int   Totalitemscount) {List   Loader.loaddata ();        Datalist.addall (NewData);    Adapter.notifydatasetchanged (); }});

As above, we have used this class as an abstract class , which is a good design. Our Infinitescrolllistener class implements the Onscroll () method , but it does not implement Loadmore (), but it also implements the implementation class .

The Onscroll () method is called automatically when we scroll. Therefore, we recommend that in this method, do not carry out excessive processing and data calculation , because the scrolling is very frequent, this method call is also very frequent.

To achieve this, we just need to implement infinitescrolllistener and set this implementation class to Listview/gridview's Setonscrolllistener () method, just like the anonymous class of the second code fragment.

We implement the Infinitescrolllistener class, we also need to implement the Loadmore () method, in which we can add item to Listview/gridview and pass Over Notifydatasetchanged () notifies Listview/gridview that the data has changed. Of course we can add local data manually or load more data from a database or server.

This is what we do in Android that can be infinitely scrollable in Listview/gridview. For a lot of information, Listview/gridview undoubtedly has a good source user experience.

Android Advanced UI Design note 09:android How to implement an infinite scrolling list

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.