一 般大家估計都很少會遇到需要LisView翻頁的問題,但是我們現在做的項目就遇到了這樣的需求,不是拖到ListView而是點擊按鈕進行翻頁!
於是就自己封裝了一個Adapter
先上代碼:
- 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 boolean hasNext, hasPrev; //標志是否有上一頁/下一頁
-
- 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)
-
-
- /**
- * 構造器
- * 適用於數據庫數據顯示
- * @param context 上下文
- * @param layout ListItem 布局文件
- * @param c 數據庫查詢游標
- * @param from 數據庫列標
- * @param to 對應於列標 顯示的容器
- */
- 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 采取鍵值對形式傳入參數
- * @param context
- * @param list
- * @param from
- * @param to
- */
- 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通過此方法獲知要顯示多少行內容
- * 我們即在此方法下手,每次設置一頁需要顯示的行數
- * 返回值:ListView 要顯示的行數
- */
- public int getCount()
- {
- //如果總行數小於一頁顯示的行數,返回總行數
- if(totalCount < pageSize)
- {
- return totalCount;
- }//即最後一頁不足5行(頁面行數)
- 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++)
- {
- //控件ID
- 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;
- }
-
- /**
- * 返回列表是否有下一頁
- * @return
- */
- public boolean hasNextPg()
- {
- return pageIndex*pageSize < totalCount;
- }
-
- /**
- * 返回是否有上一頁
- * @return
- */
- public boolean hasPrevPg()
- {
- return pageIndex > 1;
- }
-
- /**
- * 下翻頁,如果有下一頁則返回成功
- * @return
- */
- public boolean pgDown()
- {
- if(!hasNextPg())
- return false;
-
- pageIndex++;
- this.notifyDataSetChanged();
-
- return true;
- }
-
- /**
- * 上翻頁
- * @return
- */
- public boolean pgUp()
- {
- if(!hasPrevPg())
- return false;
-
- pageIndex--;
- this.notifyDataSetChanged();
-
- return true;
- }
-
- /**
- * 跳轉到某個頁碼
- * @param pageIndex
- */
- 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();
- }
-
- /**
- * 獲取一頁行數
- * @return
- */
- public int getPageSize()
- {
- return pageSize;
- }
-
- /**
- * 設置一頁行數
- * @param pageSize
- */
- public void setPageSize(int pageSize)
- {
- this.pageSize = pageSize;
- }
-
- /**
- * 獲取總行數
- * @return
- */
- public int getTotalCount()
- {
- return totalCount;
- }
-
- /**
- * 獲取當前顯示的頁碼
- * @return
- */
- public int getPageIndex()
- {
- return pageIndex;
- }
-
- /**
- * 顯示/影藏行號
- * !未實現
- * @param show
- */
- public void showLineNum(boolean show)
- {
- this.showLineNum = show;
- this.notifyDataSetChanged();
- }
- }