編輯:關於 Android 程式設計
PullToRefresh 是一套設計相當成熟的下拉刷新庫,支援的 View 類型很廣:
ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager
等常見需要刷新的容器,而且接入方式相當簡潔。
基本使用步驟
1、在版面配置檔中加入 PullToRefresh 控制項,例如 PullToRefreshListView。
2、在 Activity 中設定 OnRefreshListener,用來回應使用者的下拉動作。
3、在監聽器的 onRefresh() 中執行真正的資料刷新,通常搭配 AsyncTask。
4、AsyncTask 取回資料後,記得呼叫 onRefreshComplete(),通知控制項刷新結束。
實例:PullToRefreshDemo
執行效果:

視圖結構:

版面配置:activity_main.xml
Java 原始碼:MainActivity.java
package com.rainsong.pulltorefreshdemo;
import java.util.Arrays;
import java.util.LinkedList;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class MainActivity extends Activity {
private PullToRefreshListView mPullToRefreshListView;
private LinkedList mListItems;
private ArrayAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 當列表需要刷新時,這個監聽器會被觸發
mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(PullToRefreshBase refreshView) {
String label = DateUtils.formatDateTime(getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
// 更新 LastUpdatedLabel
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// 在這裡執行刷新列表的工作
new GetDataTask().execute();
}
});
ListView actualListView = mPullToRefreshListView.getRefreshableView();
mListItems = new LinkedList();
mListItems.addAll(Arrays.asList(mStrings));
mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mListItems);
actualListView.setAdapter(mAdapter);
}
private class GetDataTask extends AsyncTask {
@Override
protected String[] doInBackground(Void... params) {
// 模擬背景工作
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
mListItems.addFirst("Added after refresh...");
mAdapter.notifyDataSetChanged();
// 列表刷新完成後記得呼叫
mPullToRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private String[] mStrings = {
"John", "Michelle", "Amy", "Kim", "Mary", "David", "Sunny", "James",
"Maria", "Michael", "Sarah", "Robert", "Lily", "William", "Jessica",
"Paul", "Crystal", "Peter", "Jennifer", "George", "Rachel", "Thomas",
"Lisa", "Daniel", "Elizabeth", "Kevin"
};
}
Android 指南針實作:用方向感測器驅動指針旋轉
指南針看起來神奇,原理其實很單純:讀取方向感測器(Orientation Sensor)回報的角度,再讓指針圖片轉到對應角度。 一、版面配置 <?xml versi
Android 筆記 - Binder 之請求注冊 Service 組件
Android 系統大量採用了組件化的設計思路,將眾多核心服務以 Service 組件的形式對外提供。Service 只有注冊到 servicemanager 後,Cli
DrySister 看妹子應用(第二版):解析後台接口數據
一、上節回顧與本節目標 上一節我們把項目骨架搭了起來,並托管到了 GitHub。復盤之後,有兩處明顯需要改進: 1. 圖片 URL 目前是寫死的 → 改為解析接口返回的
Android 入門:LinearLayout 與 AbsoluteLayout 用法實例講解
本文以實例介紹 Android 的 LinearLayout 與 AbsoluteLayout,希望對剛入門的朋友有些幫助。 Android 的 UI 佈局都以 Lay