listview实现上一页下一页

 列表的显示需要三个元素:

1.ListVeiw 用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据    具体的将被映射的字符串,图片,或者基本组件。

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。 SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。

 

当数据罗列过多的时候,我们需要Listview分页显示,此时怎么办呢

下面一个简单的例子介绍一下

Java代码 复制代码 收藏代码
  1. package com.ideasandroid.demo;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.Gravity;   
  6. import android.view.View;   
  7. import android.view.ViewGroup;   
  8. import android.widget.BaseAdapter;   
  9. import android.widget.Button;   
  10. import android.widget.ListView;   
  11. import android.widget.TextView;   
  12.   
  13. public class listMoreTest extends Activity {   
  14.     ListView lv;   
  15.     Button btnLeft, btnRight;   
  16.        
  17.      View.OnClickListener cl;   
  18.         
  19.      MoreAdapter ma;   
  20.        
  21.      String[] data = {   
  22.              "0","1","2","3","4","5","6","7","8","9","10",   
  23.              "11","12","13","14","15","16","17","18","19","20",   
  24.              "21","22","23","24","25","26","27","28","29","30",   
  25.              "31","32","33","34","35","36","37","38","39","40",   
  26.              "41","42","43","44","45","46","47","48","49","50",   
  27.              "51","52","53","54","55","56","57","58","59","60",   
  28.             "61","62","64","64","65","66","67","68","69","70",   
  29.              "71","72","73","74","75","76","77","78","79","80",   
  30.              "81","82","83","84","85","86","87","88","89","90",   
  31.              "91","92","93","94","95","96","97","98","99","100"  
  32.      };   
  33.         
  34.      //用于显示每列5个Item项。   
  35.      int VIEW_COUNT = 5;   
  36.         
  37.      //用于显示页号的索引   
  38.      int index = 0;        
  39.      /** Called when the activity is first created. */  
  40.         
  41.      @Override  
  42.      public void onCreate(Bundle savedInstanceState) {   
  43.          super.onCreate(savedInstanceState);   
  44.          setContentView(R.layout.listview);   
  45.             
  46.          //加载Listview和2个Button   
  47.          initView();   
  48.             
  49.          //设置ListView的Adapter   
  50.          ma = new MoreAdapter(this);   
  51.          lv.setAdapter(ma);   
  52.          //此处是双向绑定吗?   
  53.             
  54.            
  55.          cl = new Button.OnClickListener(){   
  56.              @Override  
  57.              public void onClick(View v) {   
  58.                  // TODO Auto-generated method stub   
  59.                  switch(v.getId()){   
  60.                  case R.id.btnLeft:   
  61.                      leftView();   
  62.                      break;   
  63.                         
  64.                  case R.id.btnRight:   
  65.                      rightView();   
  66.                      break;   
  67.                  }   
  68.             }   
  69.   
  70.          };            
  71.          //添加2个Button的监听事件。   
  72.          btnLeft.setOnClickListener(cl);   
  73.          btnRight.setOnClickListener(cl);   
  74.             
  75.          //检查2个Button是否是可用的   
  76.          checkButton();   
  77.             
  78.      }   
  79.         
  80.      public void initView(){   
  81.          lv = (ListView)findViewById(R.id.list);   
  82.             
  83.          btnLeft = (Button)findViewById(R.id.btnLeft);   
  84.          btnRight = (Button)findViewById(R.id.btnRight);   
  85.             
  86.      }   
  87.         
  88.      //点击左边的Button,表示向前翻页,索引值要减1.   
  89.      public void leftView(){   
  90.          index--;   
  91.             
  92.          //刷新ListView里面的数值。            
  93.          ma.notifyDataSetChanged();   
  94.             
  95.          //检查Button是否可用。   
  96.          checkButton();   
  97.      }        
  98.    //点击右边的Button,表示向后翻页,索引值要加1.   
  99.      public void rightView(){   
  100.          index++;   
  101.             
  102.          //刷新ListView里面的数值。   
  103.          ma.notifyDataSetChanged();   
  104.             
  105.          //检查Button是否可用。   
  106.          checkButton();   
  107.      }   
  108.         
  109.      public void checkButton(){   
  110.          //索引值小于等于0,表示不能向前翻页了,以经到了第一页了。         //将向前翻页的按钮设为不可用。   
  111.          if(index <=0){   
  112.              btnLeft.setEnabled(false);   
  113.          }   
  114.          //值的长度减去前几页的长度,剩下的就是这一页的长度,如果这一页的长度比View_Count小,表示这是最后的一页了,后面在没有了。   
  115.          //将向后翻页的按钮设为不可用。   
  116.          else if(data.length - index*VIEW_COUNT <= VIEW_COUNT){   
  117.              btnRight.setEnabled(false);   
  118.          }   
  119.             
  120.          //否则将2个按钮都设为可用的。   
  121.          else {   
  122.             btnLeft.setEnabled(true);   
  123.              btnRight.setEnabled(true);   
  124.          }   
  125.             
  126.      }   
  127.         
  128.      //ListView的Adapter,这个是关键的导致可以分页的根本原因。   
  129.      public class MoreAdapter extends BaseAdapter {   
  130.          Activity activity;   
  131.           public MoreAdapter(Activity a){   
  132.              activity = a;   
  133.          }   
  134.             
  135.             
  136.          //设置每一页的长度,默认的是View_Count的值。   
  137.          @Override  
  138.          public int getCount() {   
  139.              // TODO Auto-generated method stub   
  140.              //return data.length               
  141.              //ori表示到目前为止的前几页的总共的个数。   
  142.              int ori = VIEW_COUNT * index;   
  143.                 
  144.              //值的总个数-前几页的个数就是这一页要显示的个数,如果比默认的值小,说明这是最后一页,只需显示这么多就可以了   
  145.              if(data.length - ori < VIEW_COUNT ){   
  146.                  return data.length - ori;   
  147.              }   
  148.              //如果比默认的值还要大,说明一页显示不完,还要用换一页显示,这一页用默认的值显示满就可以了。   
  149.             else {   
  150.                 return VIEW_COUNT;   
  151.              }   
  152.                 
  153.          }   
  154.     
  155.         @Override  
  156.         public Object getItem(int position) {   
  157.              // TODO Auto-generated method stub   
  158.              return position;   
  159.          }   
  160.     
  161.          @Override  
  162.          public long getItemId(int position) {   
  163.              // TODO Auto-generated method stub   
  164.              return position;   
  165.         }    
  166.             
  167.          @Override  
  168.          public View getView(int position, View convertView, ViewGroup parent) {   
  169.              // TODO Auto-generated method stub   
  170.              //return addTestView(position);   
  171.                 
  172.              TextView tv = new TextView(activity);   
  173.              tv.setGravity(Gravity.CENTER);   
  174.              //TextView要显示的是当前的位置+前几页已经显示的位置个数的对应的位置上的值。   
  175.              tv.setText(data[position+index*VIEW_COUNT]);   
  176.              return tv;   
  177.                 
  178.          }   
  179.      }   
  180.  }  
package com.ideasandroid.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class listMoreTest extends Activity {
    ListView lv;
    Button btnLeft, btnRight;
    
     View.OnClickListener cl;
     
     MoreAdapter ma;
    
     String[] data = {
             "0","1","2","3","4","5","6","7","8","9","10",
             "11","12","13","14","15","16","17","18","19","20",
             "21","22","23","24","25","26","27","28","29","30",
             "31","32","33","34","35","36","37","38","39","40",
             "41","42","43","44","45","46","47","48","49","50",
             "51","52","53","54","55","56","57","58","59","60",
            "61","62","64","64","65","66","67","68","69","70",
             "71","72","73","74","75","76","77","78","79","80",
             "81","82","83","84","85","86","87","88","89","90",
             "91","92","93","94","95","96","97","98","99","100"
     };
     
     //用于显示每列5个Item项。
     int VIEW_COUNT = 5;
     
     //用于显示页号的索引
     int index = 0;     
     /** Called when the activity is first created. */
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.listview);
         
         //加载Listview和2个Button
         initView();
         
         //设置ListView的Adapter
         ma = new MoreAdapter(this);
         lv.setAdapter(ma);
         //此处是双向绑定吗?
         
        
         cl = new Button.OnClickListener(){
             @Override
             public void onClick(View v) {
                 // TODO Auto-generated method stub
                 switch(v.getId()){
                 case R.id.btnLeft:
                     leftView();
                     break;
                     
                 case R.id.btnRight:
                     rightView();
                     break;
                 }
            }

         };         
         //添加2个Button的监听事件。
         btnLeft.setOnClickListener(cl);
         btnRight.setOnClickListener(cl);
         
         //检查2个Button是否是可用的
         checkButton();
         
     }
     
     public void initView(){
         lv = (ListView)findViewById(R.id.list);
         
         btnLeft = (Button)findViewById(R.id.btnLeft);
         btnRight = (Button)findViewById(R.id.btnRight);
         
     }
     
     //点击左边的Button,表示向前翻页,索引值要减1.
     public void leftView(){
         index--;
         
         //刷新ListView里面的数值。         
         ma.notifyDataSetChanged();
         
         //检查Button是否可用。
         checkButton();
     }     
   //点击右边的Button,表示向后翻页,索引值要加1.
     public void rightView(){
         index++;
         
         //刷新ListView里面的数值。
         ma.notifyDataSetChanged();
         
         //检查Button是否可用。
         checkButton();
     }
     
     public void checkButton(){
         //索引值小于等于0,表示不能向前翻页了,以经到了第一页了。         //将向前翻页的按钮设为不可用。
         if(index <=0){
             btnLeft.setEnabled(false);
         }
         //值的长度减去前几页的长度,剩下的就是这一页的长度,如果这一页的长度比View_Count小,表示这是最后的一页了,后面在没有了。
         //将向后翻页的按钮设为不可用。
         else if(data.length - index*VIEW_COUNT <= VIEW_COUNT){
             btnRight.setEnabled(false);
         }
         
         //否则将2个按钮都设为可用的。
         else {
            btnLeft.setEnabled(true);
             btnRight.setEnabled(true);
         }
         
     }
     
     //ListView的Adapter,这个是关键的导致可以分页的根本原因。
     public class MoreAdapter extends BaseAdapter {
         Activity activity;
          public MoreAdapter(Activity a){
             activity = a;
         }
         
         
         //设置每一页的长度,默认的是View_Count的值。
         @Override
         public int getCount() {
             // TODO Auto-generated method stub
             //return data.length            
             //ori表示到目前为止的前几页的总共的个数。
             int ori = VIEW_COUNT * index;
             
             //值的总个数-前几页的个数就是这一页要显示的个数,如果比默认的值小,说明这是最后一页,只需显示这么多就可以了
             if(data.length - ori < VIEW_COUNT ){
                 return data.length - ori;
             }
             //如果比默认的值还要大,说明一页显示不完,还要用换一页显示,这一页用默认的值显示满就可以了。
            else {
                return VIEW_COUNT;
             }
             
         }
 
        @Override
        public Object getItem(int position) {
             // TODO Auto-generated method stub
             return position;
         }
 
         @Override
         public long getItemId(int position) {
             // TODO Auto-generated method stub
             return position;
        } 
         
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
             // TODO Auto-generated method stub
             //return addTestView(position);
             
             TextView tv = new TextView(activity);
             tv.setGravity(Gravity.CENTER);
             //TextView要显示的是当前的位置+前几页已经显示的位置个数的对应的位置上的值。
             tv.setText(data[position+index*VIEW_COUNT]);
             return tv;
             
         }
     }
 }


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章