Android实战简易教程<二十五>(基于Baas的数据表查询下拉刷新和上拉加载实现!)

上一节我们实现了数据表的加载,但是,当数据表数据很多时,我们就要考虑数据的分页,这里我们选用了PullToRefreshListView控件,先看一下该控件的说明:

效果图: 

                               正在刷新                                                                       刷新后

      

一、导入Library

下载源码后(https://github.com/chrisbanes/Android-PullToRefresh),里面有个Library工程,添加工程到Eclipse中;

另外extras文件夹还有两个工程:PullToRefreshListFragment和PullToRefreshViewPager,由于我们的这个用不到他们的库文件,所以不必导入了;

二、实战

1、新建工程,添加Libray库到工程中

新建工程(try_PullToRefresh)后,右键-》Properties-》Android-》Add  选择上面的Library,然后就是这个样子的

2、重写activity_main.xml

XML内容为:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7. <!--     The PullToRefreshListView replaces a standard ListView widget. -->  
  8.     <com.handmark.pulltorefresh.library.PullToRefreshListView  
  9.         android:id="@+id/pull_refresh_list"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent"  
  12.         android:cacheColorHint="#00000000"  
  13.         android:divider="#19000000"  
  14.         android:dividerHeight="4dp"  
  15.         android:fadingEdge="none"  
  16.         android:fastScrollEnabled="false"  
  17.         android:footerDividersEnabled="false"  
  18.         android:headerDividersEnabled="false"  
  19.         android:smoothScrollbar="true" />  
  20.   
  21. </LinearLayout>  

其中中间那一大段<com.handmark.pull………………/>就是相当于ListView控件,用这段来代替原是ListView控件的代码

下面我们看一下具体怎么实现的。

先在数据表中插入数据:


然后看代码,MainActivity.java:

[java]  view plain copy
  1. package com.bmob.pagingdemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.widget.AbsListView;  
  14. import android.widget.AbsListView.OnScrollListener;  
  15. import android.widget.BaseAdapter;  
  16. import android.widget.ListView;  
  17. import android.widget.TextView;  
  18. import android.widget.Toast;  
  19. import cn.bmob.v3.Bmob;  
  20. import cn.bmob.v3.BmobQuery;  
  21. import cn.bmob.v3.listener.FindListener;  
  22.   
  23. import com.handmark.pulltorefresh.library.ILoadingLayout;  
  24. import com.handmark.pulltorefresh.library.PullToRefreshBase;  
  25. import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;  
  26. import com.handmark.pulltorefresh.library.PullToRefreshListView;  
  27.   
  28. public class MainActivity extends Activity {  
  29.   
  30.     PullToRefreshListView mPullToRefreshView;  
  31.     private ILoadingLayout loadingLayout;  
  32.     ListView mMsgListView;  
  33.     List<TestData> bankCards = new ArrayList<TestData>();// 数据list  
  34.   
  35.     private static final int STATE_REFRESH = 0;// 下拉刷新  
  36.     private static final int STATE_MORE = 1;// 加载更多  
  37.   
  38.     private int limit = 10// 每页的数据是10条  
  39.     private int curPage = 0// 当前页的编号,从0开始  
  40.   
  41.     @Override  
  42.     protected void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.activity_main);  
  45.   
  46.         Bmob.initialize(this"8f3ffb2658d8a3366a70a0b0ca0b71b2");// 初始化  
  47.         queryData(0, STATE_REFRESH);  
  48.   
  49.         initListView();// 初始化ListView  
  50.     }  
  51.   
  52.       
  53.     private void initListView() {  
  54.         mPullToRefreshView = (PullToRefreshListView) findViewById(R.id.list);  
  55.         loadingLayout = mPullToRefreshView.getLoadingLayoutProxy();  
  56.         loadingLayout.setLastUpdatedLabel("");  
  57.         loadingLayout  
  58.                 .setPullLabel(getString(R.string.pull_to_refresh_bottom_pull));// 下拉标签  
  59.         loadingLayout  
  60.                 .setRefreshingLabel(getString(R.string.pull_to_refresh_bottom_refreshing));// 刷新标签  
  61.         loadingLayout  
  62.                 .setReleaseLabel(getString(R.string.pull_to_refresh_bottom_release));// 释放标签  
  63.         // //滑动监听  
  64.         mPullToRefreshView.setOnScrollListener(new OnScrollListener() {  
  65.   
  66.             @Override  
  67.             public void onScrollStateChanged(AbsListView view, int scrollState) {  
  68.   
  69.             }  
  70.   
  71.             @Override  
  72.             public void onScroll(AbsListView view, int firstVisibleItem,  
  73.                     int visibleItemCount, int totalItemCount) {  
  74.   
  75.                 if (firstVisibleItem == 0) {  
  76.                     loadingLayout.setLastUpdatedLabel("");  
  77.                     loadingLayout  
  78.                             .setPullLabel(getString(R.string.pull_to_refresh_top_pull));  
  79.                     loadingLayout  
  80.                             .setRefreshingLabel(getString(R.string.pull_to_refresh_top_refreshing));  
  81.                     loadingLayout  
  82.                             .setReleaseLabel(getString(R.string.pull_to_refresh_top_release));  
  83.                 } else if (firstVisibleItem + visibleItemCount + 1 == totalItemCount) {// 加载完毕  
  84.                     loadingLayout.setLastUpdatedLabel("");  
  85.                     loadingLayout  
  86.                             .setPullLabel(getString(R.string.pull_to_refresh_bottom_pull));  
  87.                     loadingLayout  
  88.                             .setRefreshingLabel(getString(R.string.pull_to_refresh_bottom_refreshing));  
  89.                     loadingLayout  
  90.                             .setReleaseLabel(getString(R.string.pull_to_refresh_bottom_release));  
  91.                 }  
  92.             }  
  93.         });  
  94.   
  95.         // 下拉刷新监听  
  96.         mPullToRefreshView  
  97.                 .setOnRefreshListener(new OnRefreshListener2<ListView>() {  
  98.   
  99.                     @Override  
  100.                     public void onPullDownToRefresh(  
  101.                             PullToRefreshBase<ListView> refreshView) {  
  102.                         // 下拉刷新(从第一页开始装载数据)  
  103.                         queryData(0, STATE_REFRESH);  
  104.                     }  
  105.   
  106.                     @Override  
  107.                     public void onPullUpToRefresh(  
  108.                             PullToRefreshBase<ListView> refreshView) {  
  109.                         // 上拉加载更多(加载下一页数据)  
  110.                         queryData(curPage, STATE_MORE);  
  111.                     }  
  112.                 });  
  113.   
  114.         mMsgListView = mPullToRefreshView.getRefreshableView();  
  115.         // 再设置adapter  
  116.         mMsgListView.setAdapter(new DeviceListAdapter(this));  
  117.     }  
  118.   
  119.     /** 
  120.      * 分页获取数据 
  121.      *  
  122.      * @param page 
  123.      *            页码 
  124.      * @param actionType 
  125.      *            ListView的操作类型(下拉刷新、上拉加载更多) 
  126.      */  
  127.     private void queryData(final int page, final int actionType) {  
  128.         Log.i("bmob""pageN:" + page + " limit:" + limit + " actionType:"  
  129.                 + actionType);  
  130.   
  131.         BmobQuery<TestData> query = new BmobQuery<TestData>();  
  132.         query.setLimit(limit); // 设置每页多少条数据  
  133.         query.setSkip(page * limit); // 从第几条数据开始,  
  134.         query.findObjects(thisnew FindListener<TestData>() {  
  135.   
  136.             @Override  
  137.             public void onSuccess(List<TestData> arg0) {  
  138.                 // TODO Auto-generated method stub  
  139.   
  140.                 if (arg0.size() > 0) {//能加载到数据  
  141.                     if (actionType == STATE_REFRESH) {  
  142.                         // 当是下拉刷新操作时,将当前页的编号重置为0,并把bankCards清空,重新添加  
  143.                         curPage = 0;  
  144.                         bankCards.clear();  
  145.                     }  
  146.   
  147.                     // 将本次查询的数据添加到bankCards中  
  148.                     for (TestData td : arg0) {  
  149.                         bankCards.add(td);  
  150.                     }  
  151.   
  152.                     // 这里在每次加载完数据后,将当前页码+1,这样在上拉刷新的onPullUpToRefresh方法中就不需要操作curPage了  
  153.                     curPage++;  
  154.                     showToast("第" + (page + 1) + "页数据加载完成");  
  155.                 } else if (actionType == STATE_MORE) {//数据加载完毕  
  156.                     showToast("没有更多数据了");  
  157.                 } else if (actionType == STATE_REFRESH) {//无数据  
  158.                     showToast("没有数据");  
  159.                 }  
  160.                 mPullToRefreshView.onRefreshComplete();  
  161.             }  
  162.   
  163.             @Override  
  164.             public void onError(int arg0, String arg1) {  
  165.                 // TODO Auto-generated method stub  
  166.                 showToast("查询失败:" + arg1);  
  167.                 mPullToRefreshView.onRefreshComplete();  
  168.             }  
  169.         });  
  170.     }  
  171.   
  172.     /** 
  173.      * Adapter 
  174.      *  
  175.      * @author Administrator 
  176.      *  
  177.      */  
  178.     private class DeviceListAdapter extends BaseAdapter {  
  179.   
  180.         Context context;  
  181.   
  182.         public DeviceListAdapter(Context context) {  
  183.             this.context = context;  
  184.         }  
  185.   
  186.         @Override  
  187.         public View getView(final int position, View convertView,  
  188.                 ViewGroup parent) {  
  189.             ViewHolder holder = null;  
  190.             if (convertView == null) {  
  191.   
  192.                 convertView = LayoutInflater.from(context).inflate(  
  193.                         R.layout.list_item_bankcard, null);  
  194.                 holder = new ViewHolder();  
  195.                 holder.tv_cardNumber = (TextView) convertView  
  196.                         .findViewById(R.id.tv_cardNumber);  
  197.                 convertView.setTag(holder);  
  198.             } else {  
  199.                 holder = (ViewHolder) convertView.getTag();  
  200.             }  
  201.   
  202.             TestData td = (TestData) getItem(position);  
  203.   
  204.             holder.tv_cardNumber.setText(td.getName());  
  205.             return convertView;  
  206.         }  
  207.   
  208.         class ViewHolder {  
  209.             TextView tv_cardNumber;  
  210.         }  
  211.   
  212.         @Override  
  213.         public int getCount() {  
  214.             return bankCards.size();  
  215.         }  
  216.   
  217.         @Override  
  218.         public Object getItem(int position) {  
  219.             return bankCards.get(position);  
  220.         }  
  221.   
  222.         @Override  
  223.         public long getItemId(int position) {  
  224.             return position;  
  225.         }  
  226.   
  227.     }  
  228.   
  229.     private void showToast(String msg) {  
  230.         Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();  
  231.     }  
  232.   
  233. }  

TestData.java:

[html]  view plain copy
  1. package com.bmob.pagingdemo;  
  2.   
  3. import cn.bmob.v3.BmobObject;  
  4.   
  5. public class TestData extends BmobObject {  
  6.     private String name;  
  7.   
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.   
  12.     public void setName(String name) {  
  13.         this.name = name;  
  14.     }  
  15.       
  16. }  
main.xml:

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <com.handmark.pulltorefresh.library.PullToRefreshListView  
  8.         xmlns:ptr="http://schemas.android.com/apk/res/com.bmob.pagingdemo"  
  9.         android:id="@+id/list"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:fadingEdge="none"  
  13.         android:fastScrollEnabled="false"  
  14.         android:smoothScrollbar="true"  
  15.         ptr:ptrMode="both" />  
  16.   
  17. </LinearLayout>  

AndroidManifest.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.bmob.pagingdemo"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="14"  
  9.         android:targetSdkVersion="14" />  
  10.   
  11.     <uses-permission android:name="android.permission.INTERNET" /> <!-- 允许应用打开网络套接口 -->  
  12.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  13.   
  14.     <application  
  15.         android:allowBackup="true"  
  16.         android:icon="@drawable/ic_launcher"  
  17.         android:label="@string/app_name"  
  18.         android:theme="@style/AppTheme" >  
  19.         <activity  
  20.             android:name=".MainActivity"  
  21.             android:label="@string/app_name" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>  
  28.     </application>  
  29.   
  30. </manifest>  


运行实例,下拉刷新:


上拉加载,每次加载10条数据:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值