Android--listview Paging

Source: Internet
Author: User

Reference blog: Android ListView page Load (server side +android) Demo

Monitoring Onscrolllistener Events

  classOnlistscrolllistenerImplementsabslistview.onscrolllistener{@Override Public voidOnscrollstatechanged (Abslistview view,intscrollstate) {            /*** Load more data when paging operation Is_divpage is true, sliding stops, and pageno<=4 (here because the server has 4 pages of data)*/            if(is_divpage && scrollstate = = AbsListView.OnScrollListener.SCROLL_STATE_IDLE && Mpageno <=4) {//Toast.maketext (Getactivity (), "Getting more data ...", Toast.length_short). Show ();                NewMyTask (). Execute (emp_path+Mpageno); }Else if(Mpageno >4) {                /*** If pageno>4 indicates that there is no more data on the server to load. */Toast.maketext (Getactivity (),"No more data ...", Toast.length_short). Show (); }        }        /*** When: the first visible item (FIRSTVISIBLEITEM) + Visible Item number (VISIBLEITEMCOUNT) = ALL item Total, * Is_divpage becomes true, this time only         Load the data. */@Override Public voidOnscroll (Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) {Is_divpage= (Firstvisibleitem + VisibleItemCount = =totalitemcount); }    }

Background Thread requests data

  /*** MyTask inheritance thread pool Asynctask used for network data request, JSON parsing, data update and other operations. */    classMyTaskextendsAsynctask<string, Void, string> {        /*** Display dialog before data request. */@Overrideprotected voidOnPreExecute () {Super. OnPreExecute ();        Dialog.show (); }        /*** In the Doinbackground method, do some time-consuming operations such as network requests. */@Overrideprotectedstring Doinbackground (String ... params) {//return RequestData ();Gethistorydata (); return"1"; }        /*** In this method, mainly to do some data processing, update. */@Overrideprotected voidOnPostExecute (String result) {Super. OnPostExecute (Result); if(Result! =NULL) {                //If the result data obtained is not NULL, it is parsed by JSON. and displayed on the phone screen. Mdatalist.addall (mperdatalist);                Mdataadapter.binddata (mdatalist); /*** When PageNo equals 1, it will be setadapter, will not be set again, direct notifydatasetchanged, data Update *, so as to avoid the time of loading more data                 , it will go back to the first page. *///if (Mpageno = = 1) {//Mlistview.setadapter (mdataadapter);//                }mdataadapter.notifydatasetchanged (); Mpageno++; } Else if(Result = =NULL) {Toast.maketext (Getactivity (),"Request data Failed ...", Toast.length_long). Show ();        } Dialog.dismiss (); }    }

Android--listview Paging

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.