首先,寫一個xml檔案,moredata.xml,該檔案即定義了放在listview底部的檢視:
<?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" >
<Button
android:id="@+id/bt_load"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="載入更多資料" />
<ProgressBar
android:id="@+id/pg"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone"
/>
</LinearLayout>
寫一個item.xml,大家應該很熟悉了。用來定義listview的每個item的檢視。
<TextView
android:id="@+id/tv_title"
android:textSize="20sp"
android:layout_marginTop="5dp"
android:textSize="12sp"
android:id="@+id/tv_content"
main.xml就不貼了,整個主介面就一個listview。
直接先看下Activity的程式碼,在裡面實現分頁效果。
package com.notice.moredate;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MoreDateListActivity extends Activity implements OnScrollListener {
// ListView的Adapter
private SimpleAdapter mSimpleAdapter;
private ListView lv;
private Button bt;
private ProgressBar pg;
private ArrayList<HashMap<String,String>> list;
// ListView底部View
private View moreView;
private Handler handler;
// 設定一個最大的資料條數,超過即不再載入
private int MaxDateNum;
// 最後可見條目的索引
private int lastVisibleIndex;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MaxDateNum = 22; // 設定最大資料條數
lv = (ListView) findViewById(R.id.lv);
// 例項化底部佈局
moreView = getLayoutInflater().inflate(R.layout.moredate, null);
bt = (Button) moreView.findViewById(R.id.bt_load);
pg = (ProgressBar) moreView.findViewById(R.id.pg);
handler = new Handler();
// 用map來裝載資料,初始化10條資料
list = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < 10; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle", "第" + i + "行標題");
map.put("ItemText", "第" + i + "行內容");
list.add(map);
}
// 例項化SimpleAdapter
mSimpleAdapter = new SimpleAdapter(this, list, R.layout.item,
new String[] { "ItemTitle", "ItemText" },
new int[] { R.id.tv_title, R.id.tv_content });
// 加上底部View,注意要放在setAdapter方法前
lv.addFooterView(moreView);
lv.setAdapter(mSimpleAdapter);
// 繫結監聽器
lv.setOnScrollListener(this);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pg.setVisibility(View.VISIBLE);// 將進度條可見
handler.postDelayed(new Runnable() {
public void run() {
loadMoreDate();// 載入更多資料
bt.setVisibility(View.VISIBLE);
pg.setVisibility(View.GONE);
mSimpleAdapter.notifyDataSetChanged();// 通知listView重新整理資料
}, 2000);
});
private void loadMoreDate() {
int count = mSimpleAdapter.getCount();
if (count + 5 < MaxDateNum) {
// 每次載入5條
for (int i = count; i < count + 5; i++) {
map.put("ItemTitle", "新增第" + i + "行標題");
map.put("ItemText", "新增第" + i + "行內容");
首先,寫一個xml檔案,moredata.xml,該檔案即定義了放在listview底部的檢視:
<?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" >
<Button
android:id="@+id/bt_load"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="載入更多資料" />
<ProgressBar
android:id="@+id/pg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone"
/>
</LinearLayout>
寫一個item.xml,大家應該很熟悉了。用來定義listview的每個item的檢視。
<?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_title"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
<TextView
android:textSize="12sp"
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
</LinearLayout>
main.xml就不貼了,整個主介面就一個listview。
直接先看下Activity的程式碼,在裡面實現分頁效果。
package com.notice.moredate;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MoreDateListActivity extends Activity implements OnScrollListener {
// ListView的Adapter
private SimpleAdapter mSimpleAdapter;
private ListView lv;
private Button bt;
private ProgressBar pg;
private ArrayList<HashMap<String,String>> list;
// ListView底部View
private View moreView;
private Handler handler;
// 設定一個最大的資料條數,超過即不再載入
private int MaxDateNum;
// 最後可見條目的索引
private int lastVisibleIndex;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MaxDateNum = 22; // 設定最大資料條數
lv = (ListView) findViewById(R.id.lv);
// 例項化底部佈局
moreView = getLayoutInflater().inflate(R.layout.moredate, null);
bt = (Button) moreView.findViewById(R.id.bt_load);
pg = (ProgressBar) moreView.findViewById(R.id.pg);
handler = new Handler();
// 用map來裝載資料,初始化10條資料
list = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < 10; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle", "第" + i + "行標題");
map.put("ItemText", "第" + i + "行內容");
list.add(map);
}
// 例項化SimpleAdapter
mSimpleAdapter = new SimpleAdapter(this, list, R.layout.item,
new String[] { "ItemTitle", "ItemText" },
new int[] { R.id.tv_title, R.id.tv_content });
// 加上底部View,注意要放在setAdapter方法前
lv.addFooterView(moreView);
lv.setAdapter(mSimpleAdapter);
// 繫結監聽器
lv.setOnScrollListener(this);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pg.setVisibility(View.VISIBLE);// 將進度條可見
handler.postDelayed(new Runnable() {
@Override
public void run() {
loadMoreDate();// 載入更多資料
bt.setVisibility(View.VISIBLE);
pg.setVisibility(View.GONE);
mSimpleAdapter.notifyDataSetChanged();// 通知listView重新整理資料
}
}, 2000);
}
});
}
private void loadMoreDate() {
int count = mSimpleAdapter.getCount();
if (count + 5 < MaxDateNum) {
// 每次載入5條
for (int i = count; i < count + 5; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle", "新增第" + i + "行標題");
map.put("ItemText", "新增第" + i + "行內容");
list.add(map);