zoukankan      html  css  js  c++  java
  • ListView的分页显示

      java文件:

    1 package com.anddev.ListMore.Test;
    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
    40 /** Called when the activity is first created. */
    41 @Override
    42 public void onCreate(Bundle savedInstanceState) {
    43 super.onCreate(savedInstanceState);
    44 setContentView(R.layout.main);
    45
    46 //加载Listview和2个Button
    47 initView();
    48
    49 //设置ListView的Adapter
    50 ma = new MoreAdapter(this);
    51 lv.setAdapter(ma);
    52
    53
    54 cl = new Button.OnClickListener(){
    55 @Override
    56 public void onClick(View v) {
    57 // TODO Auto-generated method stub
    58 switch(v.getId()){
    59 case R.id.btnLeft:
    60 leftView();
    61 break;
    62
    63 case R.id.btnRight:
    64 rightView();
    65 break;
    66 }
    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
    99 //点击右边的Button,表示向后翻页,索引值要加1.
    100 public void rightView(){
    101 index++;
    102
    103 //刷新ListView里面的数值。
    104 ma.notifyDataSetChanged();
    105
    106 //检查Button是否可用。
    107 checkButton();
    108 }
    109
    110 public void checkButton(){
    111 //索引值小于等于0,表示不能向前翻页了,以经到了第一页了。
    112 //将向前翻页的按钮设为不可用。
    113 if(index <=0){
    114 btnLeft.setEnabled(false);
    115 }
    116 //值的长度减去前几页的长度,剩下的就是这一页的长度,如果这一页的长度比View_Count小,表示这是最后的一页了,后面在没有了。
    117 //将向后翻页的按钮设为不可用。
    118 else if(data.length - index*VIEW_COUNT <= VIEW_COUNT){
    119 btnRight.setEnabled(false);
    120 }
    121
    122 //否则将2个按钮都设为可用的。
    123 else {
    124 btnLeft.setEnabled(true);
    125 btnRight.setEnabled(true);
    126 }
    127
    128 }
    129
    130 //ListView的Adapter,这个是关键的导致可以分页的根本原因。
    131 public class MoreAdapter extends BaseAdapter {
    132 Activity activity;
    133
    134 public MoreAdapter(Activity a){
    135 activity = a;
    136 }
    137
    138
    139 //设置每一页的长度,默认的是View_Count的值。
    140 @Override
    141 public int getCount() {
    142 // TODO Auto-generated method stub
    143 //return data.length;
    144
    145 //ori表示到目前为止的前几页的总共的个数。
    146 int ori = VIEW_COUNT * index;
    147
    148 //值的总个数-前几页的个数就是这一页要显示的个数,如果比默认的值小,说明这是最后一页,只需显示这么多就可以了
    149 if(data.length - ori < VIEW_COUNT ){
    150 return data.length - ori;
    151 }
    152 //如果比默认的值还要大,说明一页显示不完,还要用换一页显示,这一页用默认的值显示满就可以了。
    153 else {
    154 return VIEW_COUNT;
    155 }
    156
    157 }
    158
    159 @Override
    160 public Object getItem(int position) {
    161 // TODO Auto-generated method stub
    162 return position;
    163 }
    164
    165 @Override
    166 public long getItemId(int position) {
    167 // TODO Auto-generated method stub
    168 return position;
    169 }
    170
    171 @Override
    172 public View getView(int position, View convertView, ViewGroup parent) {
    173 // TODO Auto-generated method stub
    174 //return addTestView(position);
    175
    176 TextView tv = new TextView(activity);
    177 tv.setGravity(Gravity.CENTER);
    178 //TextView要显示的是当前的位置+前几页已经显示的位置个数的对应的位置上的值。
    179 tv.setText(data[position+index*VIEW_COUNT]);
    180 return tv;
    181
    182 }
    183 }
    184 }
    布局文件:

    1 <?xml version="1.0" encoding="utf-8"?>
    2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:orientation="vertical"
    4 android:layout_width="fill_parent"
    5 android:layout_height="fill_parent"
    6 >
    7 <ListView android:id="@+id/list"
    8 android:layout_width="fill_parent"
    9 android:layout_height="wrap_content"
    10 />
    11 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    12 android:orientation="horizontal"
    13 android:layout_width="fill_parent"
    14 android:layout_height="wrap_content"
    15 android:gravity="bottom"
    16 >
    17 <Button android:id="@+id/btnLeft"
    18 android:layout_width="150dip"
    19 android:layout_height="wrap_content"
    20 android:text="@string/textLeft"
    21 />
    22 <Button android:id="@+id/btnRight"
    23 android:layout_width="150dip"
    24 android:layout_height="wrap_content"
    25 android:text="@string/textRight"
    26 />
    27 </LinearLayout>
    28 </LinearLayout>
    29
    结果显示:

  • 相关阅读:
    Redis总结
    设计模式-单例模式
    spring5源码解读
    关于asp.net MVC 中的TryUpdateModel方法
    WebSocket在ASP.NET MVC4中的简单实现 (该文章转自网络,经尝试并未实现,请大神指点。)
    C# 新特性 dynamic的使用及扩展
    C#用反射判断一个类型是否是Nullable同时获取它的根类型(转自网络)
    C#通用类型转换 Convert.ChangeType 转自网络
    浅谈.NET反射机制的性能优化 转自网络 博客园
    浅析大型网站的架构 转自软件中国
  • 原文地址:https://www.cnblogs.com/shang53880/p/1945587.html
Copyright © 2011-2022 走看看