上一节我们实现了数据表的加载,但是,当数据表数据很多时,我们就要考虑数据的分页,这里我们选用了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内容为:
- <?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" >
-
-
- <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>();
-
- private static final int STATE_REFRESH = 0;
- private static final int STATE_MORE = 1;
-
- private int limit = 10;
- private int curPage = 0;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Bmob.initialize(this, "8f3ffb2658d8a3366a70a0b0ca0b71b2");
- queryData(0, STATE_REFRESH);
-
- initListView();
- }
-
-
- 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() {
-
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
-
- }
-
- @Override
- public 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>() {
-
- @Override
- public void onPullDownToRefresh(
- PullToRefreshBase<ListView> refreshView) {
-
- queryData(0, STATE_REFRESH);
- }
-
- @Override
- public void onPullUpToRefresh(
- PullToRefreshBase<ListView> refreshView) {
-
- queryData(curPage, STATE_MORE);
- }
- });
-
- mMsgListView = mPullToRefreshView.getRefreshableView();
-
- mMsgListView.setAdapter(new DeviceListAdapter(this));
- }
-
-
-
-
-
-
-
-
-
- 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>() {
-
- @Override
- public void onSuccess(List<TestData> arg0) {
-
-
- if (arg0.size() > 0) {
- if (actionType == STATE_REFRESH) {
-
- curPage = 0;
- bankCards.clear();
- }
-
-
- for (TestData td : arg0) {
- bankCards.add(td);
- }
-
-
- curPage++;
- showToast("第" + (page + 1) + "页数据加载完成");
- } else if (actionType == STATE_MORE) {
- showToast("没有更多数据了");
- } else if (actionType == STATE_REFRESH) {
- showToast("没有数据");
- }
- mPullToRefreshView.onRefreshComplete();
- }
-
- @Override
- public void onError(int arg0, String arg1) {
-
- showToast("查询失败:" + arg1);
- mPullToRefreshView.onRefreshComplete();
- }
- });
- }
-
-
-
-
-
-
-
- private class DeviceListAdapter extends BaseAdapter {
-
- Context context;
-
- public DeviceListAdapter(Context context) {
- this.context = context;
- }
-
- @Override
- public 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;
- }
-
- @Override
- public int getCount() {
- return bankCards.size();
- }
-
- @Override
- public Object getItem(int position) {
- return bankCards.get(position);
- }
-
- @Override
- public 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条数据: