ListView的分页显示
生活随笔
收集整理的這篇文章主要介紹了
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 結果顯示:
?
?
轉載于:https://www.cnblogs.com/shang53880/archive/2011/01/26/1945587.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的ListView的分页显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UUID和GUID
- 下一篇: Documentum常见问题4—如何通过