android listview分页

来源:互联网 发布:剑三明教成男捏脸数据 编辑:程序博客网 时间:2024/05/16 04:48

layout中的 list_item.xml源码:

 
01<spanstyle="font-size:13px;"><?xmlversion="1.0"encoding="utf-8"?>
02<LinearLayout
03  xmlns:android="http://schemas.android.com/apk/res/android"
04  android:layout_width="fill_parent"
05  android:layout_height="fill_parent"
06  android:orientation="vertical">
07  <TextView
08     android:id="@+id/newstitle"
09     android:layout_width="fill_parent"
10     android:layout_height="wrap_content"/>
11  <TextView
12     android:id="@+id/newscontent"
13     android:layout_width="fill_parent"
14     android:layout_height="wrap_content"/>
15</LinearLayout></span>
layout中loadmore.xml源码
01<?xmlversion="1.0"encoding="utf-8"?>
02<LinearLayout
03  xmlns:android="http://schemas.android.com/apk/res/android"
04  android:layout_width="fill_parent"
05  android:layout_height="fill_parent">
06  <Button
07      android:id="@+id/loadMoreButton"
08      android:layout_width="fill_parent"
09      android:layout_height="wrap_content"
10      android:text="查看更多..."/>
11</LinearLayout>
layout中main.xml源码:
 
01<?xmlversion="1.0"encoding="utf-8"?>
02<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
03    android:orientation="vertical"
04    android:layout_width="fill_parent"
05    android:layout_height="fill_parent">
06    <ListView
07       android:id="@+id/lvNews"
08       android:layout_width="fill_parent"
09       android:layout_height="wrap_content"/>
10</LinearLayou
包 com.andyidea.bean中News.java类源码:
 
01package com.andyidea.bean;
02 
03/**
04* 新闻实体类
05* @author Andy.Chen
06* @mail Chenjunjun.ZJ@gmail.com
07*
08*/
09public class News {
10   
11    privateString title;    //标题
12    privateString content;  //内容
13   
14    publicString getTitle() {
15        returntitle;
16    }
17    publicvoid setTitle(String title) {
18        this.title = title;
19    }
20    publicString getContent() {
21        returncontent;
22    }
23    publicvoid setContent(String content) {
24        this.content = content;
25    }
26 
27}
包com.andyidea.listview中paginationListViewActivity.java类源码:
 
001package com.andyidea.listview;
002 
003import java.util.ArrayList;
004import java.util.List;
005 
006import com.andyidea.bean.News;
007 
008import android.app.Activity;
009import android.os.Bundle;
010import android.os.Handler;
011import android.util.Log;
012import android.view.View;
013import android.view.ViewGroup;
014import android.widget.AbsListView;
015import android.widget.AbsListView.OnScrollListener;
016import android.widget.BaseAdapter;
017import android.widget.Button;
018import android.widget.ListView;
019import android.widget.TextView;
020import android.widget.Toast;
021 
022public class PaginationListViewActivity extendsActivity implementsOnScrollListener {
023   
024    privateListView listView; 
025    privateint visibleLastIndex =0;   //最后的可视项索引 
026    privateint visibleItemCount;      // 当前窗口可见项总数 
027    privateint datasize = 38;          //模拟数据集的条数
028    privatePaginationAdapter adapter; 
029    privateView loadMoreView; 
030    privateButton loadMoreButton; 
031    privateHandler handler = newHandler();
032   
033    /** Called when the activity is first created. */
034    @Override
035    publicvoid onCreate(Bundle savedInstanceState) {
036        super.onCreate(savedInstanceState);
037        setContentView(R.layout.main);
038       
039        loadMoreView = getLayoutInflater().inflate(R.layout.loadmore,null);
040        loadMoreButton = (Button)loadMoreView.findViewById(R.id.loadMoreButton);
041        loadMoreButton.setOnClickListener(newView.OnClickListener() {
042           
043            @Override
044            publicvoid onClick(View v) {
045                loadMoreButton.setText("正在加载中...");  //设置按钮文字
046                handler.postDelayed(newRunnable() {
047                   
048                    @Override
049                    publicvoid run() {
050                        loadMoreData();
051                        adapter.notifyDataSetChanged();
052                        loadMoreButton.setText("查看更多..."); //恢复按钮文字
053                    }
054                },2000);
055               
056            }
057        });
058       
059        listView = (ListView)findViewById(R.id.lvNews);
060        listView.addFooterView(loadMoreView);   //设置列表底部视图
061        initializeAdapter();
062        listView.setAdapter(adapter);
063        listView.setOnScrollListener(this);
064    }
065   
066    @Override
067    publicvoid onScrollStateChanged(AbsListView view,int scrollState) {
068        intitemsLastIndex = adapter.getCount()-1//数据集最后一项的索引 
069        intlastIndex = itemsLastIndex + 1;
070        if(scrollState == OnScrollListener.SCROLL_STATE_IDLE
071                && visibleLastIndex == lastIndex) {
072            // 如果是自动加载,可以在这里放置异步加载数据的代码
073        }
074    }
075 
076 
077    @Override
078    publicvoid onScroll(AbsListView view,int firstVisibleItem,
079            intvisibleItemCount, inttotalItemCount) {
080        this.visibleItemCount = visibleItemCount;
081        visibleLastIndex = firstVisibleItem + visibleItemCount -1;
082       
083        Log.e("========================= ","========================");
084        Log.e("firstVisibleItem = ",firstVisibleItem+"");
085        Log.e("visibleItemCount = ",visibleItemCount+"");
086        Log.e("totalItemCount = ",totalItemCount+"");
087        Log.e("========================= ","========================");
088       
089        //如果所有的记录选项等于数据集的条数,则移除列表底部视图
090        if(totalItemCount == datasize+1){
091            listView.removeFooterView(loadMoreView);
092            Toast.makeText(this,"数据全部加载完!", Toast.LENGTH_LONG).show();
093        }
094    }
095   
096    /**
097     * 初始化ListView的适配器
098     */
099    privatevoid initializeAdapter(){
100        List<News> news =new ArrayList<News>();
101        for(inti=1;i<=10;i++){
102            News items =new News();
103            items.setTitle("Title"+i);
104            items.setContent("This is News Content"+i);
105            news.add(items);
106        }
107        adapter =new PaginationAdapter(news);
108    }
109   
110    /**
111     * 加载更多数据
112     */
113    privatevoid loadMoreData(){
114        intcount = adapter.getCount();
115       
116        if(count+10<= datasize){
117            for(inti=count+1; i<=count+10; i++){
118                News item =new News();
119                item.setTitle("Title"+i);
120                item.setContent("This is News Content"+i);
121                adapter.addNewsItem(item);
122            }
123        }else{
124            for(inti=count+1; i<=datasize; i++){
125                News item =new News();
126                item.setTitle("Title"+i);
127                item.setContent("This is News Content"+i);
128                adapter.addNewsItem(item);
129            }
130        }
131       
132    }
133   
134   
135    classPaginationAdapter extendsBaseAdapter{
136       
137        List<News> newsItems;
138       
139        publicPaginationAdapter(List<News> newsitems){
140            this.newsItems = newsitems;
141        }
142 
143        @Override
144        publicint getCount() {
145            returnnewsItems.size();
146        }
147 
148        @Override
149        publicObject getItem(intposition) {
150            returnnewsItems.get(position);
151        }
152 
153        @Override
154        publiclong getItemId(intposition) {
155            returnposition;
156        }
157 
158        @Override
159        publicView getView(intposition, View view, ViewGroup parent) {
160            if(view ==null){
161                view = getLayoutInflater().inflate(R.layout.list_item,null);
162            }
163           
164            //新闻标题
165            TextView tvTitle = (TextView)view.findViewById(R.id.newstitle);
166            tvTitle.setText(newsItems.get(position).getTitle());
167            //新闻内容
168            TextView tvContent = (TextView)view.findViewById(R.id.newscontent);
169            tvContent.setText(newsItems.get(position).getContent());
170           
171            returnview;
172        }
173       
174        /**
175         * 添加数据列表项
176         * @param newsitem
177         */
178        publicvoid addNewsItem(News newsitem){
179            newsItems.add(newsitem);
180        }
181       
182    }
183 
184}

原创粉丝点击