Android中ListView通过loadMore按钮或者下拉到底部加载数据

  Android应用开发中,采用ListView组件来展示数据是很常用的功能,当一个应用要展现很多的数据时,一般情况下都不会把所有的数据一次就展示出来,而是通过分页的形式来展示数据,个人觉得这样会有更好的用户体验。因此,很多应用都是采用分批次加载的形式来获取用户所需的数据。例如:微博客户端可能会在用户滑动至列表底端时自动加载下一页数据,也可能在底部放置一个"查看更多"按钮,用户点击后,加载下一页数据。

           下面通过一个Demo来展示ListView功能如何实现:该Demo通过在ListView列表的底部添加一个“查看更多...”按钮来加载新闻(模拟新闻客户端)分页数据。同时限定每次加载10条记录,但完全加载完数据后,就把ListView列表底部视图“查看更多...”删除。假设加载的数据总数为 38 条记录。先看下该Demo工程的程序结构图:


其中包 com.andyidea.bean中News.java类是新闻实体类,包com.andyidea.listview中paginationListViewActivity.java类是用来展示ListView列表。布局layout中包含三个布局文件,分别为:list_item.xml , loadmore.xml , main.xml 。下面分别贴下源码:

layout中的 list_item.xml源码:

[html]  view plain copy
  1. <span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent"  
  6.   android:orientation="vertical">  
  7.   <TextView  
  8.      android:id="@+id/newstitle"  
  9.      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源码:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">  
  6.   <Button    
  7.       android:id="@+id/loadMoreButton"    
  8.       android:layout_width="fill_parent"    
  9.       android:layout_height="wrap_content"  
  10.       android:text="查看更多..." />   
  11. </LinearLayout>  

layout中main.xml源码:

[html]  view plain copy
  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.     <ListView  
  7.        android:id="@+id/lvNews"  
  8.        android:layout_width="fill_parent"  
  9.        android:layout_height="wrap_content"/>  
  10. </LinearLayou  

包 com.andyidea.bean中News.java类 源码:

[html]  view plain copy
  1. package com.andyidea.bean;  
  2.   
  3. /**  
  4.  * 新闻实体类  
  5.  * @author Andy.Chen  
  6.  * @mail Chenjunjun.ZJ@gmail.com  
  7.  *  
  8.  */  
  9. public class News {  
  10.       
  11.     private String title;    //标题  
  12.     private String content;  //内容  
  13.       
  14.     public String getTitle() {  
  15.         return title;  
  16.     }  
  17.     public void setTitle(String title) {  
  18.         this.title = title;  
  19.     }  
  20.     public String getContent() {  
  21.         return content;  
  22.     }  
  23.     public void setContent(String content) {  
  24.         this.content = content;  
  25.     }  
  26.   
  27. }  

包com.andyidea.listview中paginationListViewActivity.java类 源码:

[html]  view plain copy
  1. package com.andyidea.listview;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.andyidea.bean.News;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.widget.AbsListView;  
  15. import android.widget.AbsListView.OnScrollListener;  
  16. import android.widget.BaseAdapter;  
  17. import android.widget.Button;  
  18. import android.widget.ListView;  
  19. import android.widget.TextView;  
  20. import android.widget.Toast;  
  21.   
  22. public class PaginationListViewActivity extends Activity implements OnScrollListener {  
  23.       
  24.     private ListView listView;    
  25.     private int visibleLastIndex = 0;   //最后的可视项索引    
  26.     private int visibleItemCount;       // 当前窗口可见项总数    
  27.     private int datasize = 38;          //模拟数据集的条数  
  28.     private PaginationAdapter adapter;    
  29.     private View loadMoreView;    
  30.     private Button loadMoreButton;    
  31.     private Handler handler = new Handler();   
  32.       
  33.     /** Called when the activity is first created. */  
  34.     @Override  
  35.     public void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.main);  
  38.           
  39.         loadMoreView = getLayoutInflater().inflate(R.layout.loadmore, null);  
  40.         loadMoreButton = (Button)loadMoreView.findViewById(R.id.loadMoreButton);  
  41.         loadMoreButton.setOnClickListener(new View.OnClickListener() {  
  42.               
  43.             @Override  
  44.             public void onClick(View v) {  
  45.                 loadMoreButton.setText("正在加载中...");   //设置按钮文字  
  46.                 handler.postDelayed(new Runnable() {  
  47.                       
  48.                     @Override  
  49.                     public void run() {  
  50.                         loadMoreData();  
  51.                         adapter.notifyDataSetChanged();  
  52.                         loadMoreButton.setText("查看更多...");  //恢复按钮文字  
  53.                     }  
  54.                 },2000);  
  55.                   
  56.             }  
  57.         });  
  58.           
  59.         listView = (ListView)findViewById(R.id.lvNews);  
  60.         listView.addFooterView(loadMoreView);    //设置列表底部视图  
  61.         initializeAdapter();  
  62.         listView.setAdapter(adapter);  
  63.         listView.setOnScrollListener(this);  
  64.     }  
  65.       
  66.     @Override  
  67.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  68.         int itemsLastIndex = adapter.getCount()-1;  //数据集最后一项的索引    
  69.         int lastIndex = itemsLastIndex + 1;  
  70.         if (scrollState == OnScrollListener.SCROLL_STATE_IDLE  
  71.                 && visibleLastIndex == lastIndex) {  
  72.             // 如果是自动加载,可以在这里放置异步加载数据的代码  
  73.         }  
  74.     }  
  75.   
  76.   
  77.     @Override  
  78.     public void onScroll(AbsListView view, int firstVisibleItem,  
  79.             int visibleItemCount, int totalItemCount) {  
  80.         this.visibleItemCount = visibleItemCount;  
  81.         visibleLastIndex = firstVisibleItem + visibleItemCount - 1;  
  82.           
  83.         Log.e("========================= ","========================");  
  84.         Log.e("firstVisibleItem = ",firstVisibleItem+"");  
  85.         Log.e("visibleItemCount = ",visibleItemCount+"");  
  86.         Log.e("totalItemCount = ",totalItemCount+"");  
  87.         Log.e("========================= ","========================");  
  88.           
  89.         //如果所有的记录选项等于数据集的条数,则移除列表底部视图  
  90.         if(totalItemCount == datasize+1){  
  91.             listView.removeFooterView(loadMoreView);  
  92.             Toast.makeText(this, "数据全部加载完!", Toast.LENGTH_LONG).show();  
  93.         }  
  94.     }  
  95.       
  96.     /**  
  97.      * 初始化ListView的适配器  
  98.      */  
  99.     private void initializeAdapter(){  
  100.         List<News> news = new ArrayList<News>();  
  101.         for(int i=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.     private void loadMoreData(){  
  114.         int count = adapter.getCount();  
  115.           
  116.         if(count+10 <= datasize){  
  117.             for(int i=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(int i=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.     class PaginationAdapter extends BaseAdapter{  
  136.           
  137.         List<News> newsItems;  
  138.           
  139.         public PaginationAdapter(List<News> newsitems){  
  140.             this.newsItems = newsitems;  
  141.         }  
  142.   
  143.         @Override  
  144.         public int getCount() {  
  145.             return newsItems.size();  
  146.         }  
  147.   
  148.         @Override  
  149.         public Object getItem(int position) {  
  150.             return newsItems.get(position);  
  151.         }  
  152.   
  153.         @Override  
  154.         public long getItemId(int position) {  
  155.             return position;  
  156.         }  
  157.   
  158.         @Override  
  159.         public View getView(int position, 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.             return view;  
  172.         }  
  173.           
  174.         /**  
  175.          * 添加数据列表项  
  176.          * @param newsitem  
  177.          */  
  178.         public void addNewsItem(News newsitem){  
  179.             newsItems.add(newsitem);  
  180.         }  
  181.           
  182.     }  
  183.   
  184. }  

最后,运行程序的结果截图如下:


通过上面的截图,当我们点击"查看更多..."按钮时,就会加载下10条记录,当加载完所有的记录后,ListView的底部视图将会移除。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值