ListView 分页显示(转载+修改)上
实习工作中,分配到了一个给已经上线的android成品增加需求的任务,其中一项是给每个信息显示增加分页显示的功能(ListView的显示),于是上网查资料,看到了:
原地址:http://www.cnblogs.com/shang53880
尚弦月大大的文章,在使用了尚大大的代码后,确实解决了问题,但是在使用过程中,发现了其中的问题,自己做了一些修改,待会将 如何修改、 项目源码 在下一篇文章中 贴出来:
尚大大的文章:
package com.anddev.ListMore.Test; 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.main); //加载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; }
}
}
布局文件:
<?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/btnLeft"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="@string/textLeft"
/>
<Button android:id="@+id/btnRight"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="@string/textRight"
/>
</LinearLayout>
</LinearLayout>
结果显示:



尚大大出现的问题,如何修改,项目源码:看这里
ListView 分页显示(转载+修改)上的更多相关文章
- ListView分页显示
出在:http://blog.csdn.net/tu_bingbing/article/details/13275107 当ListView要显示的数据过多时,为了更快的响应用户,这个 ...
- ListView 分页显示(转载+修改)下
通过实践发现,尚大大在判断”上一页“按钮和”下一页按钮“是否可用的地方,有问题: 原代码: public void checkButton(){ //索引值小于等于0,表示不能向前翻页了,以经到了第一 ...
- Sqlite 数据库分页查询(ListView分页显示数据)
下面介绍一下我的这个demo. 流程简述: 我在raw文件夹下面放了名称为city的数据库,里面包含全国2330个城市,以及所属省,拼音简写等信息. 首先 在进入MainActivity的时候,创建数 ...
- ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示(一)
为什么使用ListView+DataPager的方式实现分页显示? .net提供的诸多数据绑定控件,每一种都有它自己的优点和缺点.如果需要对数据进行操作,如果数据量不大的情况下,DataList和Gr ...
- 织梦仿站列表页pagelist分页显示竖排,如何修改成横排?
织梦仿站列表页pagelist分页显示竖排,如何修改成横排? 织梦列表页的分页标签是采用pagelist来进行调用的,但是很多人在调用之后会出现一个列表竖着排列的问题(横排美观度好一些),还是非常不美 ...
- ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示(二)
使用ListView控件进行修改,删除与添加操作1.页面代码: <asp:ListView ID="lv2" runat="server" onpagep ...
- ListView 分页 排序、编辑、插入和删除
摘自网络地址:http://msdn.microsoft.com/zh-cn/magazine/cc337984.aspx ListView 基础 ListView 是模板驱动的控件,这意味着它默认情 ...
- 转:JSP 分页显示数据 (Oracle)
JSP 分页显示数据 (Oracle) 标签: Oracle分页JSP分页 2013-11-19 20:40 3598人阅读 评论(1) 收藏 举报 分类: Web(11) 版权声明:本文为博主原 ...
- day70 cookie & session 前后端交互分页显示
本文转载自qimi博客,cnblog.liwenzhou.com 概要: 我们的cookie是保存在浏览器中的键值对 为什么要有cookie? 我们在访问浏览器的时候,千万个人访问同一个页面,我们只要 ...
随机推荐
- 在Windows系统使用Gpg4win进行加密解密
GPG,又称为GnuPG,全称是Gnu Private Guard,即GNU隐私卫士.GPG是以PGP算法为核心的强大的加密软件.但GPG项目是一套命令行程序,而且是为 Linux 等开源操作系统设计 ...
- python的os.path.join()
在python中,os.path.join()是用来拼接目录路径得.同类型得还有join(),os.path.spilt(),spilt()三个函数.1,os.path.join(),将join()里 ...
- 几乎相同的 deal.jsp 代码(index.jsp不变),在IDEA相同项目运行,结果却不一样,实在想不出来
目录 主要问题 主要项目 index.jsp: deal.jsp(正确可运行): deal.jsp(错误不可运行): 错误的代码运行图片: 可运行的代码运行图片 主要问题 几乎相同的 deal.jsp ...
- (转)maven怎么 引入(或引用/使用) 自定义(或本地/第三方) jar的三种方式 图文教程 方法二最简单
转:https://blog.csdn.net/wabiaozia/article/details/52798194 准备工作: 假如我有一个自定义jar是:123456.jar,下载地址http:/ ...
- php开发面试题---禁用cookie之后,如何使用session
php开发面试题---禁用cookie之后,如何使用session 一.总结 一句话总结: 在每个url后面自动加上PHPSESSID的值即可,用户禁止cookie后,服务器仍会将sessionId以 ...
- rabbitmq for C#的异步消息确认机制
代码: using (var conn = RabbitmqHelper.GetConnection()) { using (var channel = conn.CreateModel()) { / ...
- 五. jenkins部署springboot项目(2)--windows环境--服务
前提:jenkins和springboot运行在同一台机器 springboot 通过winsw部署为服务 winsw 下载地址:https://github.com/kohsuke/winsw/re ...
- getAttribute 与getParmeter 区别
1.getAttribute是取得jsp中 用setAttribute設定的attribute 2.parameter得到的是string:attribute得到的是object 3.request. ...
- day 101 天
一.新建项目 +安装bootstrap 安装bootstrap组件 二.Vue-route的使用 1. router.js配置文件 2. vue文件 3. Header.js文件
- FWT公式一览
总表 真值表 对应运算 FWT IFWT A=B=0 A≠B A=B=1 左项 右项 左项 右项 0 0 1 & L+R R L-R R 0 1 0 ^ L+R L-R (L+R)/2 (L- ...