Android基础教程:可翻页的 ListView 适配器——PageableAdapter


一  般大家估计都很少会遇到需要LisView翻页的问题,但是我们现在做的项目就遇到了这样的需求,不是拖到ListView而是点击按钮进行翻页!

于是就自己封装了一个Adapter

先上代码:

  1. import java.util.HashMap;  
  2. import java.util.List;  
  3.   
  4. import Android.content.Context;  
  5. import android.database.Cursor;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.TextView;  
  11.   
  12. /** 
  13.  * 可翻页ListView适配器 
  14.  * 本适配器目前支持两种数据来源,一个是数据库Cursor 一个是普通字符串List 
  15.  * 使用者可以自行扩充本适配器,应满足需求! 
  16.  * 
  17.  * @author 祝建宾 
  18.  * Create date 2011/8/9 
  19.  * Version 1.0 
  20.  * 
  21.  */  
  22. public class PageableAdapter extends BaseAdapter  
  23. {  
  24.     public static final int DATA_FROM_CURSOR = 1;  
  25.     public static final int DATA_FROM_LIST = 2;  
  26.       
  27.     private static final int DEF_PAGESIZE = 5;  
  28.       
  29.     private int totalCount;    //内容总条数   
  30.     private int pageSize;    //一页显示的行数   
  31.     private int pageIndex;    //当前显示的页码   
  32.     private int pageCount;    //总页数   
  33.     private int srcType;    //数据来源   
  34.       
  35.     private boolean showLineNum;    //显示行标   
  36. //    private boolean hasNext, hasPrev;    //标志是否有上一页/下一页   
  37.       
  38.     private Context context;  
  39.     private LayoutInflater mInflater;    //布局文件解析器   
  40.     private int layout;        //布局文件资源ID   
  41.       
  42.     private Cursor cursor;    //数据库查询游标   
  43.     private List<? extends HashMap<String, ?>> list;    //List数据来源   
  44.       
  45.     private String[] from;    //数据来源标志   
  46.     private int[] to;    //数据去向 (显示控件ID)   
  47.        
  48.       
  49.     /** 
  50.      * 构造器 
  51.      * 适用于数据库数据显示 
  52.      * @param context 上下文 
  53.      * @param layout ListItem 布局文件 
  54.      * @param c 数据库查询游标 
  55.      * @param from 数据库列标 
  56.      * @param to 对应于列标 显示的容器 
  57.      */  
  58.     public PageableAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to)  
  59.     {  
  60.         super();  
  61.           
  62.         this.context = context;  
  63.         this.layout = layout;  
  64.         this.cursor = cursor;  
  65.         this.from = from;  
  66.         this.to = to;  
  67.         //获取系统布局文件解析器   
  68.         this.mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  69.           
  70.         //数据初始化   
  71.         srcType = DATA_FROM_CURSOR;  
  72.         totalCount = cursor.getCount();  
  73.         pageSize = DEF_PAGESIZE;  
  74.         showLineNum = true;  
  75.         pageIndex = 1;  
  76.         countPage();  
  77.     }  
  78.   
  79.     /** 
  80.      * 适配器 
  81.      * 数据来源 List 继承自HashMap 采取键值对形式传入参数 
  82.      * @param context 
  83.      * @param list 
  84.      * @param from 
  85.      * @param to 
  86.      */  
  87.     public PageableAdapter(Context context, List<? extends HashMap<String, ?>> list, String[] from, int[] to)  
  88.     {  
  89.         super();  
  90.         this.context = context;  
  91.         this.list = list;  
  92.         this.from = from;  
  93.         this.to = to;  
  94.           
  95.         //数据初始化   
  96.         srcType = DATA_FROM_LIST;  
  97.         totalCount = list.size();  
  98.         pageSize = DEF_PAGESIZE;  
  99.         showLineNum = true;  
  100.         pageIndex = 1;  
  101.         countPage();  
  102.     }  
  103.   
  104.     /** 
  105.      * 内部方法,计算总页数 
  106.      */  
  107.     private void countPage()  
  108.     {  
  109.         pageCount = totalCount/pageSize;    //   
  110.         if(totalCount%pageSize > 0)    //最后一页不足pagesize个   
  111.             pageCount++;  
  112.     }  
  113.       
  114.     /** 
  115.      * ListView通过此方法获知要显示多少行内容 
  116.      * 我们即在此方法下手,每次设置一页需要显示的行数 
  117.      * 返回值:ListView 要显示的行数 
  118.      */  
  119.     public int getCount()  
  120.     {  
  121.         //如果总行数小于一页显示的行数,返回总行数   
  122.         if(totalCount < pageSize)  
  123.         {  
  124.             return totalCount;  
  125.         }//即最后一页不足5行(页面行数)   
  126.         else if(totalCount < pageIndex*pageSize)  
  127.         {  
  128.             return (totalCount-(pageIndex-1)*pageSize);  
  129.         }else    //其他情况返回页面尺寸   
  130.         {  
  131.             return pageSize;  
  132.         }  
  133.     }  
  134.   
  135.     public Object getItem(int position)  
  136.     {  
  137.         return position;  
  138.     }  
  139.   
  140.     public long getItemId(int position)  
  141.     {  
  142.         return position;  
  143.     }  
  144.   
  145.     /** 
  146.      * 获取每一项item 
  147.      */  
  148.     public View getView(int position, View convertView, ViewGroup parent)  
  149.     {  
  150.         convertView = mInflater.inflate(layout, null);  
  151.         int index = position + pageSize*(pageIndex-1);    //position对应到数据集中的正确位置   
  152.           
  153.         //将各个要显示的值部署到显示控件   
  154.         for(int i=0; i<Math.min(to.length, from.length); i++)  
  155.         {  
  156.             //控件ID   
  157.             TextView tv = (TextView)convertView.findViewById(to[i]);  
  158.               
  159.             //要显示的内容   
  160.             String text = null;  
  161.             switch (srcType)  
  162.             {  
  163.                 case DATA_FROM_CURSOR:    //cursor获取数据   
  164.                 {  
  165.                     cursor.moveToPosition(index);  
  166.                     text = cursor.getString(cursor.getColumnIndex(from[i]));  
  167.                     break;  
  168.                 }  
  169.   
  170.                 case DATA_FROM_LIST:    //list获取数据   
  171.                 {  
  172.                     HashMap<String, ?> map;  
  173.                     map = list.get(index);  
  174.                     text = (String)map.get(from[i]).toString();  
  175.                     break;  
  176.                 }  
  177.             }  
  178.             tv.setText(text);    //设置textview显示文本   
  179.         }  
  180.           
  181.         return convertView;  
  182.     }  
  183.       
  184.     /** 
  185.      * 返回列表是否有下一页 
  186.      * @return 
  187.      */  
  188.     public boolean hasNextPg()  
  189.     {  
  190.         return pageIndex*pageSize < totalCount;  
  191.     }  
  192.       
  193.     /** 
  194.      * 返回是否有上一页 
  195.      * @return 
  196.      */  
  197.     public boolean hasPrevPg()  
  198.     {  
  199.         return pageIndex > 1;  
  200.     }  
  201.       
  202.     /** 
  203.      * 下翻页,如果有下一页则返回成功 
  204.      * @return 
  205.      */  
  206.     public boolean pgDown()  
  207.     {  
  208.         if(!hasNextPg())  
  209.             return false;  
  210.           
  211.         pageIndex++;  
  212.         this.notifyDataSetChanged();  
  213.           
  214.         return true;  
  215.     }  
  216.       
  217.     /** 
  218.      * 上翻页 
  219.      * @return 
  220.      */  
  221.     public boolean pgUp()  
  222.     {  
  223.         if(!hasPrevPg())  
  224.             return false;  
  225.           
  226.         pageIndex--;  
  227.         this.notifyDataSetChanged();  
  228.           
  229.         return true;  
  230.     }  
  231.       
  232.     /** 
  233.      * 跳转到某个页码 
  234.      * @param pageIndex 
  235.      */  
  236.     public void gotoPageIndex(int pageIndex)  
  237.     {  
  238.         this.pageIndex = pageIndex;  
  239.         this.notifyDataSetChanged();  
  240.     }  
  241.       
  242.     /** 
  243.      * 跳到第一页 
  244.      */  
  245.     public void gotoFirstPg()  
  246.     {  
  247.         if(pageIndex != 1)  
  248.         {  
  249.             pageIndex = 1;  
  250.             this.notifyDataSetChanged();  
  251.         }  
  252.     }  
  253.       
  254.     /** 
  255.      * 跳到最后一页 
  256.      */  
  257.     public void gotoLastPg()  
  258.     {  
  259.         this.pageIndex = 1;  
  260.         this.notifyDataSetChanged();  
  261.     }  
  262.   
  263.     /** 
  264.      * 获取一页行数 
  265.      * @return 
  266.      */  
  267.     public int getPageSize()  
  268.     {  
  269.         return pageSize;  
  270.     }  
  271.   
  272.     /** 
  273.      * 设置一页行数 
  274.      * @param pageSize 
  275.      */  
  276.     public void setPageSize(int pageSize)  
  277.     {  
  278.         this.pageSize = pageSize;  
  279.     }  
  280.   
  281.     /** 
  282.      * 获取总行数 
  283.      * @return 
  284.      */  
  285.     public int getTotalCount()  
  286.     {  
  287.         return totalCount;  
  288.     }  
  289.   
  290.     /** 
  291.      * 获取当前显示的页码 
  292.      * @return 
  293.      */  
  294.     public int getPageIndex()  
  295.     {  
  296.         return pageIndex;  
  297.     }  
  298.   
  299.     /** 
  300.      * 显示/影藏行号 
  301.      * !未实现 
  302.      * @param show 
  303.      */  
  304.     public void showLineNum(boolean show)  
  305.     {  
  306.         this.showLineNum = show;  
  307.         this.notifyDataSetChanged();  
  308.     }  
  309. }  

相关内容