歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android基礎教程:可翻頁的 ListView 適配器——PageableAdapter

Android基礎教程:可翻頁的 ListView 適配器——PageableAdapter

日期:2017/3/1 10:52:47   编辑:Linux編程

一 般大家估計都很少會遇到需要LisView翻頁的問題,但是我們現在做的項目就遇到了這樣的需求,不是拖到ListView而是點擊按鈕進行翻頁!

於是就自己封裝了一個Adapter

先上代碼:

  1. import java.util.HashMap;
  2. import java.util.List;
  3. import Android.content.Context;
  4. import android.database.Cursor;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.TextView;
  10. /**
  11. * 可翻頁ListView適配器
  12. * 本適配器目前支持兩種數據來源,一個是數據庫Cursor 一個是普通字符串List
  13. * 使用者可以自行擴充本適配器,應滿足需求!
  14. *
  15. * @author 祝建賓
  16. * Create date 2011/8/9
  17. * Version 1.0
  18. *
  19. */
  20. public class PageableAdapter extends BaseAdapter
  21. {
  22. public static final int DATA_FROM_CURSOR = 1;
  23. public static final int DATA_FROM_LIST = 2;
  24. private static final int DEF_PAGESIZE = 5;
  25. private int totalCount; //內容總條數
  26. private int pageSize; //一頁顯示的行數
  27. private int pageIndex; //當前顯示的頁碼
  28. private int pageCount; //總頁數
  29. private int srcType; //數據來源
  30. private boolean showLineNum; //顯示行標
  31. // private boolean hasNext, hasPrev; //標志是否有上一頁/下一頁
  32. private Context context;
  33. private LayoutInflater mInflater; //布局文件解析器
  34. private int layout; //布局文件資源ID
  35. private Cursor cursor; //數據庫查詢游標
  36. private List<? extends HashMap<String, ?>> list; //List數據來源
  37. private String[] from; //數據來源標志
  38. private int[] to; //數據去向 (顯示控件ID)
  39. /**
  40. * 構造器
  41. * 適用於數據庫數據顯示
  42. * @param context 上下文
  43. * @param layout ListItem 布局文件
  44. * @param c 數據庫查詢游標
  45. * @param from 數據庫列標
  46. * @param to 對應於列標 顯示的容器
  47. */
  48. public PageableAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to)
  49. {
  50. super();
  51. this.context = context;
  52. this.layout = layout;
  53. this.cursor = cursor;
  54. this.from = from;
  55. this.to = to;
  56. //獲取系統布局文件解析器
  57. this.mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  58. //數據初始化
  59. srcType = DATA_FROM_CURSOR;
  60. totalCount = cursor.getCount();
  61. pageSize = DEF_PAGESIZE;
  62. showLineNum = true;
  63. pageIndex = 1;
  64. countPage();
  65. }
  66. /**
  67. * 適配器
  68. * 數據來源 List 繼承自HashMap 采取鍵值對形式傳入參數
  69. * @param context
  70. * @param list
  71. * @param from
  72. * @param to
  73. */
  74. public PageableAdapter(Context context, List<? extends HashMap<String, ?>> list, String[] from, int[] to)
  75. {
  76. super();
  77. this.context = context;
  78. this.list = list;
  79. this.from = from;
  80. this.to = to;
  81. //數據初始化
  82. srcType = DATA_FROM_LIST;
  83. totalCount = list.size();
  84. pageSize = DEF_PAGESIZE;
  85. showLineNum = true;
  86. pageIndex = 1;
  87. countPage();
  88. }
  89. /**
  90. * 內部方法,計算總頁數
  91. */
  92. private void countPage()
  93. {
  94. pageCount = totalCount/pageSize; //
  95. if(totalCount%pageSize > 0) //最後一頁不足pagesize個
  96. pageCount++;
  97. }
  98. /**
  99. * ListView通過此方法獲知要顯示多少行內容
  100. * 我們即在此方法下手,每次設置一頁需要顯示的行數
  101. * 返回值:ListView 要顯示的行數
  102. */
  103. public int getCount()
  104. {
  105. //如果總行數小於一頁顯示的行數,返回總行數
  106. if(totalCount < pageSize)
  107. {
  108. return totalCount;
  109. }//即最後一頁不足5行(頁面行數)
  110. else if(totalCount < pageIndex*pageSize)
  111. {
  112. return (totalCount-(pageIndex-1)*pageSize);
  113. }else //其他情況返回頁面尺寸
  114. {
  115. return pageSize;
  116. }
  117. }
  118. public Object getItem(int position)
  119. {
  120. return position;
  121. }
  122. public long getItemId(int position)
  123. {
  124. return position;
  125. }
  126. /**
  127. * 獲取每一項item
  128. */
  129. public View getView(int position, View convertView, ViewGroup parent)
  130. {
  131. convertView = mInflater.inflate(layout, null);
  132. int index = position + pageSize*(pageIndex-1); //position對應到數據集中的正確位置
  133. //將各個要顯示的值部署到顯示控件
  134. for(int i=0; i<Math.min(to.length, from.length); i++)
  135. {
  136. //控件ID
  137. TextView tv = (TextView)convertView.findViewById(to[i]);
  138. //要顯示的內容
  139. String text = null;
  140. switch (srcType)
  141. {
  142. case DATA_FROM_CURSOR: //cursor獲取數據
  143. {
  144. cursor.moveToPosition(index);
  145. text = cursor.getString(cursor.getColumnIndex(from[i]));
  146. break;
  147. }
  148. case DATA_FROM_LIST: //list獲取數據
  149. {
  150. HashMap<String, ?> map;
  151. map = list.get(index);
  152. text = (String)map.get(from[i]).toString();
  153. break;
  154. }
  155. }
  156. tv.setText(text); //設置textview顯示文本
  157. }
  158. return convertView;
  159. }
  160. /**
  161. * 返回列表是否有下一頁
  162. * @return
  163. */
  164. public boolean hasNextPg()
  165. {
  166. return pageIndex*pageSize < totalCount;
  167. }
  168. /**
  169. * 返回是否有上一頁
  170. * @return
  171. */
  172. public boolean hasPrevPg()
  173. {
  174. return pageIndex > 1;
  175. }
  176. /**
  177. * 下翻頁,如果有下一頁則返回成功
  178. * @return
  179. */
  180. public boolean pgDown()
  181. {
  182. if(!hasNextPg())
  183. return false;
  184. pageIndex++;
  185. this.notifyDataSetChanged();
  186. return true;
  187. }
  188. /**
  189. * 上翻頁
  190. * @return
  191. */
  192. public boolean pgUp()
  193. {
  194. if(!hasPrevPg())
  195. return false;
  196. pageIndex--;
  197. this.notifyDataSetChanged();
  198. return true;
  199. }
  200. /**
  201. * 跳轉到某個頁碼
  202. * @param pageIndex
  203. */
  204. public void gotoPageIndex(int pageIndex)
  205. {
  206. this.pageIndex = pageIndex;
  207. this.notifyDataSetChanged();
  208. }
  209. /**
  210. * 跳到第一頁
  211. */
  212. public void gotoFirstPg()
  213. {
  214. if(pageIndex != 1)
  215. {
  216. pageIndex = 1;
  217. this.notifyDataSetChanged();
  218. }
  219. }
  220. /**
  221. * 跳到最後一頁
  222. */
  223. public void gotoLastPg()
  224. {
  225. this.pageIndex = 1;
  226. this.notifyDataSetChanged();
  227. }
  228. /**
  229. * 獲取一頁行數
  230. * @return
  231. */
  232. public int getPageSize()
  233. {
  234. return pageSize;
  235. }
  236. /**
  237. * 設置一頁行數
  238. * @param pageSize
  239. */
  240. public void setPageSize(int pageSize)
  241. {
  242. this.pageSize = pageSize;
  243. }
  244. /**
  245. * 獲取總行數
  246. * @return
  247. */
  248. public int getTotalCount()
  249. {
  250. return totalCount;
  251. }
  252. /**
  253. * 獲取當前顯示的頁碼
  254. * @return
  255. */
  256. public int getPageIndex()
  257. {
  258. return pageIndex;
  259. }
  260. /**
  261. * 顯示/影藏行號
  262. * !未實現
  263. * @param show
  264. */
  265. public void showLineNum(boolean show)
  266. {
  267. this.showLineNum = show;
  268. this.notifyDataSetChanged();
  269. }
  270. }
Copyright © Linux教程網 All Rights Reserved