效果图:
代码实现如下:
1、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
2、item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="22sp" android:textColor="@android:color/black" /> </LinearLayout>
3、footer.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="正在努力加载中......" android:layout_gravity="center_vertical" /> </LinearLayout>
4、DataService
package com.njupt.pageloaddata3; import java.util.ArrayList; import java.util.List; public class DataService { public List<String> getData(int startPosition,int offset){ List<String> data = new ArrayList<String>(); int i; for(i = 0 ; i < 20 ; ++i){ data.add("章泽天是我的女神...." + i); } return data; } }
5、MainActivity
package com.njupt.pageloaddata3; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.app.Activity; import android.view.Menu; 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 static final int SUCCESS_GET_DATA = 0; private ListView listview; private List<String> data; private DataService service; private ArrayAdapter adapter; private boolean finish = true; private View footer; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case SUCCESS_GET_DATA: ArrayList<String> result = (ArrayList<String>) msg.obj; data.addAll(result); adapter.notifyDataSetChanged(); finish = true; if(listview.getFooterViewsCount() > 0){ listview.removeFooterView(footer); } break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listview = (ListView) findViewById(R.id.listview); data = new ArrayList<String>(); service = new DataService(); adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tv_info, data); footer = View.inflate(this, R.layout.footer, null); listview.addFooterView(footer); listview.setAdapter(adapter); listview.removeFooterView(footer); listview.setOnScrollListener(new MyOnScrollListener()); } private class MyOnScrollListener implements OnScrollListener{ private int countPage = 5; private int pageSize =20; @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { final int totalCount = firstVisibleItem + visibleItemCount ; int currentPage = totalCount/pageSize; int nextPage = currentPage + 1; if(totalCount == totalItemCount && nextPage <= countPage && finish){ finish = false; listview.addFooterView(footer); new Thread(){ public void run() { SystemClock.sleep(10000); Message msg = new Message(); msg.what = SUCCESS_GET_DATA; msg.obj = service.getData(totalCount + 1, pageSize); handler.sendMessage(msg); }; }.start(); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }