一般來說,大家大概很少會遇到需要讓 ListView 翻頁的情況,但我們手上的專案偏偏就有這樣的需求——不是靠拖動捲動,而是要點擊按鈕來翻頁!於是我便自己封裝了一個 PageableAdapter。
這個適配器目前支援兩種資料來源:資料庫的 Cursor,以及普通的字串 List。使用者可以依需求自行擴充。以下先上程式碼:
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
* 可翻頁 ListView 適配器
* 本適配器目前支援兩種資料來源:資料庫 Cursor 與普通字串 List,
* 使用者可自行擴充本適配器以滿足需求。
*
* @author 祝建賓
* Create date 2011/8/9
* Version 1.0
*/
public class PageableAdapter extends BaseAdapter
{
public static final int DATA_FROM_CURSOR = 1;
public static final int DATA_FROM_LIST = 2;
private static final int DEF_PAGESIZE = 5;
private int totalCount; // 內容總條數
private int pageSize; // 一頁顯示的行數
private int pageIndex; // 當前顯示的頁碼
private int pageCount; // 總頁數
private int srcType; // 資料來源
private boolean showLineNum; // 顯示行標
private Context context;
private LayoutInflater mInflater; // 布局文件解析器
private int layout; // 布局文件資源 ID
private Cursor cursor; // 資料庫查詢游標
private List<? extends HashMap<String, ?>> list; // List 資料來源
private String[] from; // 資料來源標志
private int[] to; // 資料去向(顯示控件 ID)
/**
* 構造器,適用於資料庫資料顯示
*/
public PageableAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to)
{
super();
this.context = context;
this.layout = layout;
this.cursor = cursor;
this.from = from;
this.to = to;
// 取得系統布局文件解析器
this.mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 資料初始化
srcType = DATA_FROM_CURSOR;
totalCount = cursor.getCount();
pageSize = DEF_PAGESIZE;
showLineNum = true;
pageIndex = 1;
countPage();
}
/**
* 構造器,資料來源為 List(繼承自 HashMap,採鍵值對形式傳入參數)
*/
public PageableAdapter(Context context, List<? extends HashMap<String, ?>> list, String[] from, int[] to)
{
super();
this.context = context;
this.list = list;
this.from = from;
this.to = to;
// 資料初始化
srcType = DATA_FROM_LIST;
totalCount = list.size();
pageSize = DEF_PAGESIZE;
showLineNum = true;
pageIndex = 1;
countPage();
}
/**
* 內部方法,計算總頁數
*/
private void countPage()
{
pageCount = totalCount/pageSize;
if(totalCount%pageSize > 0) // 最後一頁不足 pageSize 個
pageCount++;
}
/**
* ListView 透過此方法得知要顯示多少行內容,
* 我們即在此方法下手,每次設定一頁需要顯示的行數。
*/
public int getCount()
{
// 如果總行數小於一頁顯示的行數,回傳總行數
if(totalCount < pageSize)
{
return totalCount;
} // 即最後一頁不足 pageSize 行
else if(totalCount < pageIndex*pageSize)
{
return (totalCount-(pageIndex-1)*pageSize);
} else // 其他情況回傳頁面尺寸
{
return pageSize;
}
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
/**
* 取得每一項 item
*/
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(layout, null);
int index = position + pageSize*(pageIndex-1); // position 對應到資料集中的正確位置
// 將各個要顯示的值部署到顯示控件
for(int i=0; i < Math.min(to.length, from.length); i++)
{
TextView tv = (TextView)convertView.findViewById(to[i]);
// 要顯示的內容
String text = null;
switch (srcType)
{
case DATA_FROM_CURSOR: // 從 cursor 取得資料
{
cursor.moveToPosition(index);
text = cursor.getString(cursor.getColumnIndex(from[i]));
break;
}
case DATA_FROM_LIST: // 從 list 取得資料
{
HashMap<String, ?> map;
map = list.get(index);
text = (String)map.get(from[i]).toString();
break;
}
}
tv.setText(text); // 設定 TextView 顯示文本
}
return convertView;
}
/**
* 回傳列表是否有下一頁
*/
public boolean hasNextPg()
{
return pageIndex*pageSize < totalCount;
}
/**
* 回傳是否有上一頁
*/
public boolean hasPrevPg()
{
return pageIndex > 1;
}
/**
* 下翻頁,如果有下一頁則回傳成功
*/
public boolean pgDown()
{
if(!hasNextPg())
return false;
pageIndex++;
this.notifyDataSetChanged();
return true;
}
/**
* 上翻頁
*/
public boolean pgUp()
{
if(!hasPrevPg())
return false;
pageIndex--;
this.notifyDataSetChanged();
return true;
}
/**
* 跳轉到某個頁碼
*/
public void gotoPageIndex(int pageIndex)
{
this.pageIndex = pageIndex;
this.notifyDataSetChanged();
}
/**
* 跳到第一頁
*/
public void gotoFirstPg()
{
if(pageIndex != 1)
{
pageIndex = 1;
this.notifyDataSetChanged();
}
}
/**
* 跳到最後一頁
*/
public void gotoLastPg()
{
this.pageIndex = 1;
this.notifyDataSetChanged();
}
/**
* 取得一頁行數
*/
public int getPageSize()
{
return pageSize;
}
/**
* 設定一頁行數
*/
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}
/**
* 取得總行數
*/
public int getTotalCount()
{
return totalCount;
}
/**
* 取得當前顯示的頁碼
*/
public int getPageIndex()
{
return pageIndex;
}
/**
* 顯示/隱藏行號(尚未實作)
*/
public void showLineNum(boolean show)
{
this.showLineNum = show;
this.notifyDataSetChanged();
}
}
這個適配器的巧妙之處,在於它「劫持」了 getCount() 的回傳值。原版 ListView 會依 getCount() 決定要渲染幾列,而我們讓它只回傳「目前這一頁的行數」,再於 getView() 中透過 index = position + pageSize * (pageIndex - 1) 把頁內的 position 換算成整份資料集的正確索引。如此一來,只要呼叫 pgUp()、pgDown() 或 gotoPageIndex() 改變頁碼並觸發 notifyDataSetChanged(),列表就會刷新成對應頁面的內容。
值得一提的是,gotoLastPg() 目前仍寫死為 pageIndex = 1,若要正確跳到最後一頁,應改為 pageCount,這是使用時可留意的細節。