【ListView】动态加载之一:滑动加载

代码中写了注释,这里就不做阐述。

先看一张效果图:

 

当滑动到视图底部,底部显示Loading状态,然后加载数据,数据加载完成,Loading消失,新的数据显示。对ListView滑动状态有一个监听,

Activity需继承接口:OnScrollListener。

 

下面附上源码:

MainActivity

[java]  view plain copy print ?
  1. package com.listview.jasonlee.demo;  
  2.   
  3. import java.util.List;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.AbsListView;  
  11. import android.widget.BaseAdapter;  
  12. import android.widget.ListView;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15. import android.widget.AbsListView.OnScrollListener;  
  16. import android.widget.AdapterView;  
  17. import android.widget.AdapterView.OnItemClickListener;  
  18.   
  19. public class MainActivity extends Activity implements OnScrollListener,  
  20.         OnItemClickListener {  
  21.   
  22.     private ListView listView;  
  23.     private List<String> currentData;// 当前视图显示的数据  
  24.     private CustomAdapter customadapter;// 自定义适配器  
  25.     private View loadingView;// 加载视图的布局  
  26.   
  27.     private int currentPage = 1;// 当前页,默认为1  
  28.     private int pageSize = 10;// 每页显示十条信息  
  29.     private int last_item_position;// 最后item的位置  
  30.   
  31.     private boolean isLoading = false;// 是否加载过,控制加载次数  
  32.   
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.   
  36.         setContentView(R.layout.main);  
  37.   
  38.         listView = (ListView) findViewById(R.id.lv_id);  
  39.   
  40.         // 加载视图布局  
  41.         loadingView = LayoutInflater.from(this).inflate(  
  42.                 R.layout.list_page_load, null);  
  43.         // 创建当前用于显示视图的数据  
  44.         currentData = RemoteDataUtil.createUpdateData(currentPage, pageSize);  
  45.         currentPage = currentPage + 1;  
  46.         // 添加底部加载视图  
  47.         listView.addFooterView(loadingView);  
  48.         // 初始化适配器  
  49.         customadapter = new CustomAdapter(currentData, this);  
  50.         listView.setAdapter(customadapter);  
  51.         // 滚动条监听  
  52.         listView.setOnScrollListener(this);  
  53.         // ListView监听  
  54.         listView.setOnItemClickListener(this);  
  55.     }  
  56.   
  57.     @Override  
  58.     public void onScroll(AbsListView view, int firstVisibleItem,  
  59.             int visibleItemCount, int totalItemCount) {  
  60.   
  61.         last_item_position = firstVisibleItem + visibleItemCount - 1;  
  62.   
  63.         if (last_item_position == totalItemCount - 2) {// 这里控制当焦点落在某一个位置时,开始加载.  
  64.                                                         // 当前是在第9个位置开始加载,改为totalItemCount-1  
  65.                                                         // 则会在第10个位置开始加载  
  66.               
  67.   
  68.             /** 
  69.              * Loading 标记当前视图是否处于加载中,如果正在加载(isLoading=true)就不执行更新操作 
  70.              * 加载完成后isLoading=false,在 loadingHandler 中改变状态 
  71.              */  
  72.             if (!isLoading) {  
  73.                   
  74.                 Toast.makeText(MainActivity.this"第 " + currentPage + " 页",  
  75.                         Toast.LENGTH_LONG).show();  
  76.                   
  77.                 // 开启一个线程加载数据  
  78.                 isLoading = true;  
  79.                         RemoteDataUtil.setRemoteDataByPage(currentPage, pageSize,  
  80.                                 new LoadStateInterface() {  
  81.                                     @Override  
  82.                                     public void onLoadComplete(List<String> remotDate) {  
  83.                                         customadapter.addItem(remotDate);  
  84.                                         handler.sendEmptyMessage(0);  
  85.                                     }  
  86.                                 });  
  87.                     };  
  88.         };  
  89.   
  90.         // 当ListView没有FooterView时,添加FooterView(---loadingView---)  
  91.         if (listView.getFooterViewsCount() == 0) {  
  92.             listView.addFooterView(loadingView);  
  93.         }  
  94.     }  
  95.       
  96.     Handler handler = new Handler(){  
  97.         public void handleMessage(android.os.Message msg) {  
  98.             // 更新  
  99.             customadapter.notifyDataSetChanged();  
  100.             // 删除FooterView  
  101.             listView.removeFooterView(loadingView);  
  102.             // 进入下一页,此时视图未加载.  
  103.             isLoading = false;  
  104.             //当前页自加  
  105.             currentPage = currentPage + 1;  
  106.         };  
  107.     };  
  108.   
  109.     @Override  
  110.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  111.         // TODO Auto-generated method stub  
  112.     }  
  113.   
  114.     @Override  
  115.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
  116.         // TODO Auto-generated method stub  
  117.     }  
  118. }  


 

CustomAdapter

[java]  view plain copy print ?
  1. package com.listview.jasonlee.demo;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.TextView;  
  11.   
  12. public class CustomAdapter extends BaseAdapter {  
  13.   
  14.     private List<String> currentData;// 当前视图显示的数据  
  15.   
  16.     private LayoutInflater inflater;  
  17.       
  18.     public CustomAdapter() {  
  19.         super();  
  20.         // TODO Auto-generated constructor stub  
  21.     }  
  22.   
  23.     public CustomAdapter(List<String> currentData, Context context) {  
  24.         super();  
  25.         this.currentData = currentData;  
  26.         inflater = LayoutInflater.from(context);  
  27.     }  
  28.   
  29.     @Override  
  30.     public int getCount() {  
  31.         // TODO Auto-generated method stub  
  32.         return currentData.size();  
  33.     }  
  34.   
  35.     @Override  
  36.     public Object getItem(int position) {  
  37.         // TODO Auto-generated method stub  
  38.         return position;  
  39.     }  
  40.   
  41.     @Override  
  42.     public long getItemId(int position) {  
  43.         // TODO Auto-generated method stub  
  44.         return position;  
  45.     }  
  46.   
  47.     @Override  
  48.     public View getView(int position, View convertView, ViewGroup parent) {  
  49.         View view = inflater.inflate(R.layout.list_page_item, null);  
  50.         TextView tv_content = (TextView) view.findViewById(R.id.tv_content);  
  51.         tv_content.setText(currentData.get(position));  
  52.         return view;  
  53.     }  
  54.       
  55.     public void addItem(List<String> items){  
  56.         for(String item:items){  
  57.             currentData.add(item);  
  58.         }  
  59.     }  
  60. }  

 


LoadStateInterface

[java]  view plain copy print ?
  1. package com.listview.jasonlee.demo;  
  2.   
  3. import java.util.List;  
  4.   
  5. public interface LoadStateInterface {  
  6.     /* 加载完成 */  
  7.     public void onLoadComplete(List<String> remotDate);  
  8. }  


 

 

RemoteDataUtil

[java]  view plain copy print ?
  1. package com.listview.jasonlee.demo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class RemoteDataUtil {  
  7.   
  8.     /** 
  9.      * @param currentPage 
  10.      * @param pageSize 
  11.      * @return 
  12.      * 模拟网络数据 
  13.      */  
  14.     public static List<String> createUpdateData(int currentPage, int pageSize) {  
  15.         List<String> list = new ArrayList<String>();  
  16.         for (int i = (currentPage - 1) * pageSize; i < currentPage * pageSize; i++) {  
  17.             list.add((i + 1) + ". 走在风中,今天阳光突然很温柔.");  
  18.         }  
  19.         return list;  
  20.     }  
  21.       
  22.       
  23.     // 模拟联接网络以及从网络中获取数据花费的时间  
  24.     public static void setRemoteDataByPage(final int currentPage,final int pageSize,final LoadStateInterface onLoadComplete) {  
  25.         new Thread(){  
  26.             @Override  
  27.             public void run() {  
  28.                 // TODO Auto-generated method stub  
  29.                 super.run();  
  30.                   
  31.                 try {  
  32.                     Thread.sleep(3000);  
  33.                 } catch (InterruptedException e) {  
  34.                     // TODO Auto-generated catch block  
  35.                     e.printStackTrace();  
  36.                 }  
  37.                 // 获取当前要更新的数据  
  38.                 List<String> updateDataList = createUpdateData(  
  39.                         currentPage, pageSize);  
  40.                 // 需要更新的数据加入当前数据集合  
  41.                 onLoadComplete.onLoadComplete(updateDataList);  
  42.                   
  43.             }  
  44.         }.start();  
  45.           
  46.     }  
  47. }  


 

main.xml

[html]  view plain copy print ?
  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:id="@+id/main_layout"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent">  
  7.     <ListView android:id="@+id/lv_id"  
  8.                  android:layout_width="fill_parent"  
  9.                  android:layout_height="wrap_content" />  
  10. </LinearLayout>  


 

 

list_page_item.xml

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView   
  6.         android:id="@+id/tv_content"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="80dp"  
  9.         android:textSize="9pt"  
  10.         android:gravity="center_vertical"  
  11.     />  
  12. </LinearLayout>  


 

list_page_load.xml

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/loading_layout"  
  4.     android:layout_width="fill_parent"  
  5.      android:layout_height="fill_parent"  
  6.     android:orientation="vertical"  
  7.      android:gravity="center_horizontal"  
  8.     >  
  9.     <RelativeLayout  
  10.         android:id="@+id/load_id"  
  11.         android:orientation="horizontal"   
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:gravity="center"  
  15.         >  
  16.           
  17.         <TextView   
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_alignParentLeft="true"  
  21.             android:gravity="center_vertical"  
  22.             android:text="Loading..."  
  23.             android:textSize="12pt" />  
  24.           
  25.         <ProgressBar android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:gravity="center_vertical"  
  28.             android:layout_alignParentRight="true"/>  
  29.     </RelativeLayout>  
  30. </LinearLayout>  


 

【ListView】动态加载之滑动加载 >> 源代码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值