編輯:關於 Android 程式設計
本文分兩部分:先用官方 SwipeRefreshLayout 完成下拉刷新,再說明如何用 PullToRefresh 同時支援上拉加載與下拉刷新,並解決「上拉下拉要跑不同邏輯」的問題。
先看效果,相比舊版 support 19.1.0 的橫條樣式,這版旋轉圈的作品質感提升不少,接入難度也低。

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="MergeRootFrame" > <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_container" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </android.support.v4.widget.SwipeRefreshLayout> </FrameLayout> package hi.xiaoyu.swiperefreshlayout; import hi.xiaoyu.swiperefreshlayout.adapter.TestAdapter; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; import android.widget.ListView; public class MainActivity extends Activity implements OnRefreshListener { private SwipeRefreshLayout swipeLayout; private ListView listView; private List<String> listDatas; private TestAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container); listView = (ListView) findViewById(R.id.list); swipeLayout.setOnRefreshListener(this); swipeLayout.setColorSchemeResources(android.R.color.holo_orange_dark, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); listDatas = new ArrayList<String>(); for (int i = 0; i < 10; i++) { listDatas.add("item" + i); } adapter = new TestAdapter(this, listDatas, R.layout.test_item); listView.setAdapter(adapter); } public void onRefresh() { new Handler().postDelayed(new Runnable() { public void run() { swipeLayout.setRefreshing(false); listDatas.addAll(listDatas); adapter.notifyDataSetChanged(); } }, 3000); } }
短短幾行就能跑起來,不必引入第三方 jar,缺點是官方遲遲未提供上拉加載。想補上也不難——改動原始碼加上拉判斷即可,我個人是搭配舊版的刷新效果做成上拉樣式,視覺上相當協調。
目前較成熟的選擇是 PullToRefresh,它支援 ListView、ExpandableListView、GridView、WebView 等多種控件。第一步當然是把 library 匯入專案,這部分不再贅述。
1、版面配置
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pull_refresh_list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
2、區分上拉與下拉的關鍵
實作 OnRefreshListener 的 onRefresh 就能完成刷新,但若要讓上拉與下拉執行不同業務,需要一個判斷依據。原理是:依「上拉區」與「下拉區」的可見狀態來區分方向。在 com.handmark.pulltorefresh.library 套件中找到 PullToRefreshBase.java,於類別末尾加上兩個方法:
public boolean isHeaderShown() { return getHeaderLayout().isShown(); } public boolean isFooterShown() { return getFooterLayout().isShown(); }
3、Activity 實作
public class MainActivity extends ListActivity { private PullToRefreshListView mPullToRefreshListView; private LinkedList<String> mItemList; private ArrayAdapter<String> adapter; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; initData(); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mItemList); //初始化控件 mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_refresh_list); ListView mListView = mPullToRefreshListView.getRefreshableView(); mListView.setAdapter(adapter); //設置pull-to-refresh模式為Mode.Both mPullToRefreshListView.setMode(Mode.BOTH); //設置上拉下拉事件 mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { if (refreshView.isHeaderShown()){ Toast.makeText(context, "下拉刷新",Toast.LENGTH_SHORT).show(); //下拉刷新 業務代碼 }else { Toast.makeText(context, "上拉加載更多",Toast.LENGTH_SHORT).show(); //上拉加載更多 業務代碼 } } }); } private void initData(){ //初始化數據 mItemList = new LinkedList<String>(); mItemList.addAll(Arrays.asList(data)); } private String[] data = new String[]{"data1","data2","data3","data4","data5","data6", "data1","data2","data3","data4","data5","data6"}; }
方向判斷的核心就是這段:
if (refreshView.isHeaderShown()){ Toast.makeText(context, "下拉刷新",Toast.LENGTH_SHORT).show(); //下拉刷新 業務代碼 }else { Toast.makeText(context, "上拉加載更多",Toast.LENGTH_SHORT).show(); //上拉加載更多 業務代碼 }
只需要下拉刷新,直接用官方 SwipeRefreshLayout,零依賴、樣式現代;需要上拉分頁,且專案已可接受第三方庫,PullToRefresh 比較省事。但要注意:PullToRefresh 已多年未更新,在新專案中建議改用 SwipeRefreshLayout 搭配自行實作或成熟的分頁組件(如 Paging 3),並在 onRefresh 結束後呼叫 setRefreshing(false)、在沒有更多資料時 disable 上拉載入,避免使用者拉到空轉。
AndroidManifest.xml 深度解析:activity 標籤的所有屬性
語法與位置 <manifest> <application> <activity android:name=str
Android 入門:Style 與 Theme 的用法實例解析
現在有越來越多的網路服務把客戶端搬上 Android,而為了留住使用者,這些客戶端在視覺上多半下過功夫:佈局合理、外觀耐看。這背後的關鍵之一,就是 Android 的 S
關於 ListView 的全面總結:適配器、原理與內存優化
網上關於 ListView 的介紹很多,我挑選了一些內容整合於此,僅供參考。 在 Android 開發中,ListView 是比較常用的組件。 官方 API 地址:htt
Android Fragment 與 Activity 的雙向溝通與生命週期全解析
Fragment 與 Activity 之間的互動關係 每一個 Fragment 實例都與承載它的 Activity 綁定在一起,兩者天生就存在密切的關聯。Fragmen