利用ListView进行分页

来源:互联网 发布:剑网3正太捏脸数据 编辑:程序博客网 时间:2024/04/26 04:20

main.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="bottom">
<Button android:id="@+id/pre" android:text="@string/pre"
android:layout_width="wrap_content" android:layout_height="wrap_content"
/>
<Button android:id="@+id/next" android:text="@string/next"
android:layout_width="wrap_content" android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>

string.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivit!</string>
<string name="app_name">listpage</string>
<string name="pre">上一页</string>
<string name="next">下一页</string>
<string name="page">分页</string>
</resources>

java代码如下:

package com.page;


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

public class MainActivit extends Activity {
private ListView lv;
private MyAdapter ma ;
private Button pre,next;
// 用于显示每列5个Item项。
private int VIEW_COUNT = 10;
View.OnClickListener vc ;
// 用于显示页号的索引
private int index = 0;
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" };


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
ma = new MyAdapter(this);
lv.setAdapter(ma);
vc = new OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.pre :
leftView();
break;
case R.id.next :
rightView();
break;
}
}
};

pre.setOnClickListener(vc);
next.setOnClickListener(vc);

checkButton();
}
private void initView() {
lv = (ListView)findViewById(R.id.list);
pre = (Button)findViewById(R.id.pre);
next = (Button)findViewById(R.id.next);
}


public void leftView(){
index --;
ma.notifyDataSetChanged();
checkButton();
}
public void rightView(){
index ++ ;
ma.notifyDataSetChanged();
checkButton();
}
public void checkButton(){
//索引值小于等于0,表示不能向前翻页了,以经到了第一页了。
   //将向前翻页的按钮设为不可用。
if(index <= 0){
pre.setEnabled(false);
}
//值的长度减去前几页的长度,剩下的就是这一页的长度,如果这一页的长度比View_Count小,表示这是最后的一页了,后面在没有了。
   //将向后翻页的按钮设为不可用。
else if(data.length - index * VIEW_COUNT < VIEW_COUNT){
next.setEnabled(false);
}else{
pre.setEnabled(true);
next.setEnabled(true);
}
}
/**
* ListView的Adapter,这个是关键的导致可以分页的根本原因。

* @author Administrator
*/
class MyAdapter extends BaseAdapter {
private Activity activity;


public MyAdapter(Activity activity) {
this.activity = activity;
}


// 设置每一页的长度,默认的是View_Count的值。
@Override
public int getCount() {
// ori表示到目前为止的前几页的总共的个数。
int ori = VIEW_COUNT * index;
/**
* 值的总个数-前几页的个数就是这一页要显示的个数,如果比默认的值小,说明这是最后一页, 只需显示这么多就可以了
*/
if (data.length - ori < VIEW_COUNT) {
return data.length - ori;
}
// 如果比默认的值还要大,说明一页显示不完,还要用换一页显示,这一页用默认的值显示满就可以了。
return VIEW_COUNT;
}


@Override
public Object getItem(int position) {
return position;
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(activity);
tv.setGravity(Gravity.CENTER);
// TextView要显示的是当前的位置+前几页已经显示的位置个数的对应的位置上的值。
tv.setText(data[position + index * VIEW_COUNT]);
return tv;
}


}
}
 
原创粉丝点击