Android實戰簡易教程-第二十五槍(基於Baas的資料表查詢下拉重新整理和上拉載入實現!),androidbaas
上一節我們實現了資料表的載入,但是,當資料表資料很多時,我們就要考慮資料的分頁,這裡我們選用了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 plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <!-- The PullToRefreshListView replaces a standard ListView widget. -->
- <com.handmark.pulltorefresh.library.PullToRefreshListView
- android:id="@+id/pull_refresh_list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:cacheColorHint="#00000000"
- android:divider="#19000000"
- android:dividerHeight="4dp"
- android:fadingEdge="none"
- android:fastScrollEnabled="false"
- android:footerDividersEnabled="false"
- android:headerDividersEnabled="false"
- android:smoothScrollbar="true" />
-
- </LinearLayout>
其中中間那一大段<com.handmark.pull………………/>就是相當於ListView控制項,用這段來代替原是ListView控制項的代碼
下面我們看一下具體怎麼實現的。
先在資料表中插入資料:
然後看代碼,MainActivity.java:
package com.bmob.pagingdemo;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import cn.bmob.v3.Bmob;import cn.bmob.v3.BmobQuery;import cn.bmob.v3.listener.FindListener;import com.handmark.pulltorefresh.library.ILoadingLayout;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;import com.handmark.pulltorefresh.library.PullToRefreshListView;public class MainActivity extends Activity {PullToRefreshListView mPullToRefreshView;private ILoadingLayout loadingLayout;ListView mMsgListView;List<TestData> bankCards = new ArrayList<TestData>();// 資料listprivate static final int STATE_REFRESH = 0;// 下拉重新整理private static final int STATE_MORE = 1;// 載入更多private int limit = 10; // 每頁的資料是10條private int curPage = 0; // 當前頁的編號,從0開始@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Bmob.initialize(this, "8f3ffb2658d8a3366a70a0b0ca0b71b2");// 初始化queryData(0, STATE_REFRESH);initListView();// 初始化ListView}private void initListView() {mPullToRefreshView = (PullToRefreshListView) findViewById(R.id.list);loadingLayout = mPullToRefreshView.getLoadingLayoutProxy();loadingLayout.setLastUpdatedLabel("");loadingLayout.setPullLabel(getString(R.string.pull_to_refresh_bottom_pull));// 下拉標籤loadingLayout.setRefreshingLabel(getString(R.string.pull_to_refresh_bottom_refreshing));// 重新整理標籤loadingLayout.setReleaseLabel(getString(R.string.pull_to_refresh_bottom_release));// 釋放標籤// //滑動監聽mPullToRefreshView.setOnScrollListener(new OnScrollListener() {@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {if (firstVisibleItem == 0) {loadingLayout.setLastUpdatedLabel("");loadingLayout.setPullLabel(getString(R.string.pull_to_refresh_top_pull));loadingLayout.setRefreshingLabel(getString(R.string.pull_to_refresh_top_refreshing));loadingLayout.setReleaseLabel(getString(R.string.pull_to_refresh_top_release));} else if (firstVisibleItem + visibleItemCount + 1 == totalItemCount) {// 載入完畢loadingLayout.setLastUpdatedLabel("");loadingLayout.setPullLabel(getString(R.string.pull_to_refresh_bottom_pull));loadingLayout.setRefreshingLabel(getString(R.string.pull_to_refresh_bottom_refreshing));loadingLayout.setReleaseLabel(getString(R.string.pull_to_refresh_bottom_release));}}});// 下拉重新整理監聽mPullToRefreshView.setOnRefreshListener(new OnRefreshListener2<ListView>() {@Overridepublic void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {// 下拉重新整理(從第一頁開始裝載資料)queryData(0, STATE_REFRESH);}@Overridepublic void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {// 上拉載入更多(載入下一頁資料)queryData(curPage, STATE_MORE);}});mMsgListView = mPullToRefreshView.getRefreshableView();// 再設定adaptermMsgListView.setAdapter(new DeviceListAdapter(this));}/** * 分頁擷取資料 * * @param page * 頁碼 * @param actionType * ListView的操作類型(下拉重新整理、上拉載入更多) */private void queryData(final int page, final int actionType) {Log.i("bmob", "pageN:" + page + " limit:" + limit + " actionType:"+ actionType);BmobQuery<TestData> query = new BmobQuery<TestData>();query.setLimit(limit); // 設定每頁多少條資料query.setSkip(page * limit); // 從第幾條資料開始,query.findObjects(this, new FindListener<TestData>() {@Overridepublic void onSuccess(List<TestData> arg0) {// TODO Auto-generated method stubif (arg0.size() > 0) {//能載入到資料if (actionType == STATE_REFRESH) {// 當是下拉重新整理操作時,將當前頁的編號重設為0,並把bankCards清空,重新添加curPage = 0;bankCards.clear();}// 將本次查詢的資料添加到bankCards中for (TestData td : arg0) {bankCards.add(td);}// 這裡在每次載入完資料後,將當前頁碼+1,這樣在上拉重新整理的onPullUpToRefresh方法中就不需要操作curPage了curPage++;showToast("第" + (page + 1) + "頁資料載入完成");} else if (actionType == STATE_MORE) {//資料載入完畢showToast("沒有更多資料了");} else if (actionType == STATE_REFRESH) {//無資料showToast("沒有資料");}mPullToRefreshView.onRefreshComplete();}@Overridepublic void onError(int arg0, String arg1) {// TODO Auto-generated method stubshowToast("查詢失敗:" + arg1);mPullToRefreshView.onRefreshComplete();}});}/** * Adapter * * @author Administrator * */private class DeviceListAdapter extends BaseAdapter {Context context;public DeviceListAdapter(Context context) {this.context = context;}@Overridepublic View getView(final int position, View convertView,ViewGroup parent) {ViewHolder holder = null;if (convertView == null) {convertView = LayoutInflater.from(context).inflate(R.layout.list_item_bankcard, null);holder = new ViewHolder();holder.tv_cardNumber = (TextView) convertView.findViewById(R.id.tv_cardNumber);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}TestData td = (TestData) getItem(position);holder.tv_cardNumber.setText(td.getName());return convertView;}class ViewHolder {TextView tv_cardNumber;}@Overridepublic int getCount() {return bankCards.size();}@Overridepublic Object getItem(int position) {return bankCards.get(position);}@Overridepublic long getItemId(int position) {return position;}}private void showToast(String msg) {Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();}}
TestData.java:
package com.bmob.pagingdemo;import cn.bmob.v3.BmobObject;public class TestData extends BmobObject {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res/com.bmob.pagingdemo" android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:fadingEdge="none" android:fastScrollEnabled="false" android:smoothScrollbar="true" ptr:ptrMode="both" /></LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bmob.pagingdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <uses-permission android:name="android.permission.INTERNET" /> <!-- 允許應用開啟網路套介面 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
運行執行個體,下拉重新整理:
上拉載入,每次載入10條資料:
本執行個體有個問題,初次進入時不能載入資料,必須下拉才能載入資料,請能找出問題的朋友聯絡我:291214603,多謝!
喜歡的朋友可以關注我!謝謝!
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。