Batch loading of Android data-slide to the bottom to automatically load the list

Source: Internet
Author: User
Tags addall

Batch loading of Android data-slide to the bottom to automatically load the list


May 9, 2014

This blog post describes how to load data in batches. ListView is often used in application development. It is often easy to click more to load data. To provide a user experience, when a user rolls the list to the bottom and automatically loads data, this method is used much.

The example below is that 20 pieces of data are simulated each time, and 20 pieces of data are requested at the bottom until the request reaches the limit page.

Code implementation:

/08_Datapageload/src/com/wwj/datapageload/MainActivity. java

Package com. wwj. datapageload; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. util. log; import android. view. view; import android. widget. absListView; import android. widget. absListView. onScrollListener; import android. widget. arrayAdapter; import android. widget. listView; public class MainActivity extends Activity {private ListView listView; private List
 
  
Data = new ArrayList
  
   
(); ArrayAdapter
   
    
Adapter; View footer; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); footer = getLayoutInflater (). inflate (R. layout. footer, null); listView = (ListView) findViewById (R. id. listView); listView. setOnScrollListener (new ScrollListener (); // simulate data. addAll (DataService. getData (0, 20); adapter = new ArrayAdapter
    
     
(This, R. layout. listview_item, R. id. textView, data); listView. addFooterView (footer); // Add a footer (put at the end of ListView) listView. setAdapter (adapter); listView. removeFooterView (footer);} private int number = 20; // how many pieces of data are obtained each time. private int maxpage = 5; // the total number of private boolean loadfinish = true; // indicates whether the private final class ScrollListener implements OnScrollListener {@ Overridepublic void onScroll (AbsListView view, int FirstVisibleItem, int visibleItemCount, int totalItemCount) {Log. I ("MainActivity", "onScroll (firstVisibleItem =" + firstVisibleItem + ", visibleItemCount =" + visibleItemCount + ", totalItemCount =" + totalItemCount + ")"); final int loadtotal = totalItemCount; int lastItemid = listView. getLastVisiblePosition (); // obtain the IDif (lastItemid + 1) = totalItemCount) of the last Item on the current screen {// if (totalItemCount> 0)) {// Int currentpage = totalItemCount % number = 0? TotalItemCount/number: totalItemCount/number + 1; int nextpage = currentpage + 1; // next page if (nextpage <= maxpage & loadfinish) {loadfinish = false; listView. addFooterView (footer); // open a Thread to load data. new Thread (new Runnable () {@ Overridepublic void run () {try {Thread. sleep (3000);} catch (InterruptedException e) {e. printStackTrace ();} List
     
      
Result = DataService. getData (loadtotal, number); // send the handler message. sendMessage (handler. obtainMessage (100, data ));}}). start () ;}}@ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {Log. I ("MainActivity", "onScrollStateChanged (scrollState =" + scrollState + ")") ;}} private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {data. addAll (List
      
        ) Msg. obj); // tells the ListView that the data has changed. The ListView update interface is required to display the adapter. yydatasetchanged (); if (listView. getFooterViewsCount ()> 0) {// if there is a bottom view listView. removeFooterView (footer);} loadfinish = true; // Loading completed };};}
      
     
    
   
  
 

/08_Datapageload/src/com/wwj/datapageload/DataService. java

Package com. wwj. datapageload; import java. util. ArrayList; import java. util. List; public class DataService {public static List
 
  
GetData (int offset, int maxResult) {// page limit // List
  
   
Data = new ArrayList
   
    
(); For (int I = 0; I <20; I ++) {data. add ("batch loading of ListView data" + I) ;}return data ;}}
   
  
 


As follows:


Related Article

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.