編輯:關於 Android 程式設計
PullToRefresh 是一套設計精良的下拉刷新庫,支援 ListView、ExpandableListView、GridView、WebView 等多種常見需要刷新的 View 類型,使用起來十分方便。(下載地址:https://github.com/chrisbanes/Android-PullToRefresh)
下載後將它作為 library 匯入 Eclipse,再於自己的工程中引用即可。
1. 在布局檔中加入想用的 View,例如這裡使用支援下拉刷新的 ExpandableListView:
<com.handmark.pulltorefresh.library.PullToRefreshExpandableListView android:id="@+id/expand_list" android:layout_width="match_parent" android:layout_height="match_parent" />
2. 在 Activity 中做簡單設定:
mExpandList = (PullToRefreshExpandableListView) rootView.findViewById(R.id.expand_list); mExpandList.getRefreshableView().setGroupIndicator(null); mExpandList.getRefreshableView().setDivider(null); mExpandList.getRefreshableView().setSelector(android.R.color.transparent); mExpandList.getRefreshableView().setOnGroupClickListener(this); mExpandList.setOnRefreshListener(this);
第一行找到 View,最後一行加上刷新監聽器,中間幾行是對 ExpandableListView 的額外設定。
3. 下拉刷新時執行的 onRefresh():
@Override public void onRefresh(PullToRefreshBase<ExpandableListView> refreshView) { if (!isRefreshing) { isRefreshing = true; updateList(true); } else { mExpandList.onRefreshComplete(); } }由於通常會另開執行緒取資料,這裡加了一個判斷:若已在載入中就直接 onRefreshComplete() 收起下拉;否則開新執行緒取資料,取完別忘了也要呼叫 onRefreshComplete()。
PullToRefresh 本身同時支援下拉與上拉,因此只要把上拉「改造成」上拉加載即可。
1. 設定 Mode
// set mode to BOTH mExpandList.setMode(Mode.BOTH); mExpandList.getLoadingLayoutProxy(false, true).setPullLabel(getString(R.string.pull_to_load)); mExpandList.getLoadingLayoutProxy(false, true).setRefreshingLabel(getString(R.string.loading)); mExpandList.getLoadingLayoutProxy(false, true).setReleaseLabel(getString(R.string.release_to_load));
把 Mode 設為 Mode.BOTH 後,上拉與下拉都會觸發 onRefresh()。由於上方要顯示「下拉刷新」、下方要顯示「上拉加載」,因此後三行是修改下方部分的文字,getLoadingLayoutProxy(false, true) 可依實際感受調整。
2. 如何區分下拉與上拉
網上有些做法是用 onScrollListener 判斷,但並不嚴謹。這裡依賴 header 還是 footer 處於可見狀態來區分:下拉時 header 一定可見,反之 footer 一定可見。由於 PullToRefreshExpandableListView 未提供這樣的介面,可以小改引入的庫——在 com.handmark.pulltorefresh.library 下的 PullToRefreshAdapterViewBase.java 中加入兩個方法:
public boolean isHeaderShown() { return getHeaderLayout().isShown(); } public boolean isFooterShown() { return getFooterLayout().isShown(); } 重新編譯庫工程與自己的工程後,在 onRefresh() 中這樣使用:
@Override public void onRefresh(PullToRefreshBase<ExpandableListView> refreshView) { if (!isRefreshing) { isRefreshing = true; if (mExpandList.isHeaderShown()) { Utils.LOGD("pull-to-refresh"); refreshOnlineStatus(true); } else if (mExpandList.isFooterShown()) { Utils.LOGD("pull-to-load-more"); loadNextPage(); } } else { mExpandList.onRefreshComplete(); } }如此一來,就能用 PullToRefresh 同時實現下拉刷新與上拉加載。
1.要實現上拉監聽,只需實作 OnRefreshListener2,同時別忘了 setMode(Mode.BOTH)。
2.PullToRefreshListView 有個已知 BUG:在 XML 布局中,不能一開始就把它的 visibility 設為 GONE,否則之後在程式碼中設為 VISIBLE 也不會生效。
最後放上一張效果圖:

Android 根據螢幕寬度按比例縮放圖片
ImageView 有 scaleType 屬性可以縮放圖片,讓圖片鋪滿螢幕寬度,但往往會出現壓縮或裁剪的情況。 ImageView 的 scaleType 屬性分別是
Android 動畫基礎:Drawable Animation 與 View Animation 全示例
Android 的動畫體系主要分為三大類: Drawable Animation(逐幀動畫) View Animation(補間動畫) Property Animati
自製底部 Tabbar:從 RadioGroup 方案到高擴展性的自訂控件
Tabbar 最早出現在 iOS,那邊用 TabBarController 就能輕鬆實作。現在的 App 大多用 Tabbar 作為功能導航,界面簡單清晰。Android
深入解析 Android Service:啟動、綁定與生命週期全攻略
在 Android 的四大組件之中,Service 扮演著「幕後推手」的角色。它是一個不具備使用者介面的應用元件,專門負責在背景執行長時間的操作。當某個應用元件啟動了 S