`
hududanyzd
  • 浏览: 815254 次
文章分类
社区版块
存档分类
最新评论

ListView的分页

 
阅读更多

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>

0
0
分享到:
评论

相关推荐

    ListView分页

    ListView分页是Android开发中一个常见且重要的技术,主要用于处理大量数据展示,以提高用户体验和应用性能。在Android中,ListView通常用于显示一系列可滚动的项目列表,而分页则是为了减少内存消耗和提高加载速度,...

    WPF中ListView分页

    实现ListView分页通常需要以下步骤: 1. **创建数据源**:首先,你需要一个可分页的数据源。这通常是一个包含分页信息的ObservableCollection,如PagedCollectionView或自己实现的分页数据集。 2. **设置View**:...

    android listview分页查询显示

    在这个"android listview分页查询显示"的Demo中,我们将深入探讨如何实现这个功能,以及如何结合SQLite数据库进行数据管理。 首先,ListView的工作原理是通过Adapter来连接数据源和视图。Adapter负责将数据转化为...

    listView分页显示 加载更多

    "ListView分页显示 加载更多"的功能是优化用户体验的重要手段,它允许用户在滚动到列表底部时动态加载更多的数据,而不是一次性加载所有内容,从而节省内存资源并提高应用性能。 在实现ListView分页加载更多功能时...

    Android ListView分页功能源码

    在ListView分页加载中,我们通常会结合PagerAdapter或FragmentPagerAdapter来创建一个滑动页面的效果,这样用户可以逐页浏览内容,而不是一次性看到所有数据。这两种适配器用于在ViewPager中管理页面,与ListView...

    ListView分页显示上一页和下一页

    "ListView分页显示上一页和下一页"这一主题,就是讨论如何实现ListView在用户滚动到列表底部时自动加载下一页数据,同时提供上一页的切换功能。 一、分页原理 分页的基本思想是将大量数据分为多个小部分(页)来...

    Android_ListView分页

    一、ListView分页原理 分页的基本思想是将大量数据分成若干个小部分(每部分称为一页),每次只加载一部分数据到ListView中,当用户滚动到底部时,再加载下一页的数据。这样既能保证界面的流畅性,又能有效利用系统...

    WPF ListView分页实现.doc

    ### WPF ListView分页实现详解 在Windows Presentation Foundation(WPF)中,`ListView`控件是用于展示数据集合的强大工具。当数据集过大时,为了提高应用性能和用户体验,通常会采用分页技术来控制每次加载的数据...

    简单ListView分页Demo

    本示例"简单ListView分页Demo"旨在教你如何实现一个具有分页功能的ListView,以展示不同类型的音乐,如流行音乐和古典音乐,并根据类型显示不同数量的条目。我们将探讨以下几个关键知识点: 1. **ListView基础**: ...

    ListView分页加载及图片缓存显示的实现

    本示例“ListView分页加载及图片缓存显示的实现”着重讲解如何优化ListView的性能,提高用户体验,特别是处理大量图片时。以下是对该主题的详细解释: 1. **ListView分页加载**: 分页加载(Pagination)是一种...

    安卓listview相关相关-Listview分页加载数据.rar

    本压缩包"安卓listview相关相关-Listview分页加载数据.rar"主要关注的是ListView的分页加载数据这一核心特性。 一、ListView基础 1. ListView的基本结构:ListView由多个View(如TextView、ImageView等)组成的...

    Android高级应用源码-Listview分页加载数据.zip

    本资源"Android高级应用源码-Listview分页加载数据.zip"提供了一种高效处理大量数据的方法——分页加载,这对于优化用户体验和节省系统资源至关重要。下面将详细探讨ListView分页加载的核心原理、实现方法以及其在...

    android listview分页加载的demo【源代码】

    在Android开发中,ListView是广泛...以上就是关于“android listview分页加载的demo【源代码】”的详细知识点介绍,通过学习和实践,开发者可以掌握在Android应用中高效展示大量数据的技巧,提升应用的性能和用户体验。

    Android listview分页

    要实现ListView分页,你需要做以下几步: 1. 数据源管理:创建一个适配器(如ArrayAdapter或BaseAdapter),管理当前显示的数据。当需要加载更多数据时,向适配器添加新数据。 2. 监听滚动事件:通过ListView的...

    很简单的WPF ListView分页技术

    WPF ListView如何分页困扰了我很久,百度搜索的结果几乎得不到满意的结果,而CSDN论坛上流传的资源包貌似是微软提供的例子,但是略显复杂。经过笔者一晚上的摸索,得到了一种比较简单的解决方案,在此贡献给大家。...

    Android ListView分页加载(服务端+Android端)Demo

    Android ListView分页加载功能 在实际开发中经常用到,是每个开发者必须掌握的内容,本Demo给出了服务端+Android端的两者的代码,并成功通过了测试。 服务端使用MyEclipse,Android端使用Eclipse。

    简单的listview分页

    本教程主要围绕"简单的listview分页"展开,旨在帮助初学者理解如何实现ListView的顶部或底部拉动加载更多功能,以及Item之间的分离效果。下面将详细介绍这两个关键知识点。 一、ListView分页 1. 分页原理:分页是...

    这是一个ListView分页显示的示例

    本示例"HeaderBottomListDemo"专注于实现ListView的分页加载功能,这在处理大数据集时尤为重要,因为它可以提高应用性能并优化用户体验。分页加载允许用户滚动到列表底部时加载更多数据,而不是一次性加载所有内容,...

    listview分页加载

    ListView的数据分页加载功能是提高用户体验和优化应用性能的关键技术。本篇文章将深入探讨如何在Android中实现ListView的分页加载。 首先,理解分页加载的概念至关重要。分页加载是指在用户滚动列表到底部时,应用...

    使用listview分页显示数据

    本资源提供了一个自定义ListView结合分页功能的实践案例,包括代码实现和数据库操作,对于学习和理解如何在Android应用中实现高效的数据加载和展示具有很高的参考价值。 首先,我们来了解ListView的基础知识。...

Global site tag (gtag.js) - Google Analytics