A simple Android paging data loading instance automatically loads the next page of data by sliding to the bottom of the instance. Now, you can click and drag a custom control.
The following is a major Activity code:
01 public class MainActivity extends Activity {
02 private ListView listview;
03 private View footer;
04 private List <String> data;
05 private ArrayAdapter <String> adapter;
06
07 @ Override
08 public void onCreate (Bundle savedInstanceState ){
09 super. onCreate (savedInstanceState );
10 setContentView (R. layout. main );
11 listview = (ListView) findViewById (R. id. listview );
12 // Add data to the adapter
13 data = new DataService (). getData ();
14 adapter = new ArrayAdapter <String> (this, R. layout. item, R. id. TV _name, data );
15 // you must set head and Footer before setAdapter
16 footer = getLayoutInflater (). inflate (R. layout. footer, null );
17 listview. addFooterView (footer );
18
19 // configure adpater in listview
20 listview. setAdapter (adapter );
21 listview. removeFooterView (footer );
22 // The data on the ListView can be displayed, but it cannot be displayed by page. No rolling event is added to the LIstView,
23 // next we will add a rolling event to the ListVIew to automatically refresh the data loaded by page in the ListView.
24 listview. setOnScrollListener (new MyOnScrollListener ());
25}
26
27 public final class MyOnScrollListener implements OnScrollListener {
28 protected static final int GET_DATA_SUCCESS = 0;
29 private int pageCount = 20;
30 private int pageSize = 5;
31 // whether the loading is complete
32 Boolean finish = true;
33 private Handler mHandler = new Handler (){
34 public void handleMessage (Message msg ){
35 msg. what = GET_DATA_SUCCESS;
36 List <String> result = (List <String>) msg. obj;
37 data. addAll (result );
38 finish = true;
39 if (listview. getFooterViewsCount ()> 0 ){
40 listview. removeFooterView (footer );
41}
42 // notify the adapter to update data
43 adapter. notifyDataSetChanged ();
44 };
45 };
46
47 @ Override
48 public void onScrollStateChanged (AbsListView view, int scrollState ){
49
50}
51
52 @ Override
53 public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount ){
54 // calculate the total number of items currently loaded
55 int totalItem = firstVisibleItem + visibleItemCount;
56 // calculate the current page
57 int currenPage = totalItemCount/pageSize;
58 // calculate the value of the next page
59 int nextPage = currenPage + 1;
60 // when the total number of items currently loaded is equal to the total number of items displayed, you can load
61 if (totalItem = totalItemCount ){
62 if (nextPage <pageCount & finish ){
63 finish = false;
64 listview. addFooterView (footer );
65 // start loading data
66 new Thread (){
67 public void run (){
68 SystemClock. sleep (3000 );
69 List <String> result = new DataService (). getData ();
70 Message msg = new Message ();
71 msg. what = GET_DATA_SUCCESS;
72 msg. obj = result;
73 mHandler. sendMessage (msg );
74} www.2cto.com
75
76}. start ();
77}
78}
79}
80}
81}
The following is a data service,
01 public class DataService {
02 /*
03 * a List of 20 elements is generated each time.
04 */
05 public List <String> getData (){
06 List <String> data = new ArrayList <String> ();
07 for (int I = 0; I <20; I ++ ){
08 data. add ("the id of the current item is:" + I );
09}
10 return data;
11}
12}
Author: Simpleness