Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 下拉刷新與上拉加載:官方 SwipeRefreshLayout 與 PullToRefresh 兩案對比

編輯:關於 Android 程式設計

本文分兩部分:先用官方 SwipeRefreshLayout 完成下拉刷新,再說明如何用 PullToRefresh 同時支援上拉加載與下拉刷新,並解決「上拉下拉要跑不同邏輯」的問題。

一、Android 5.0 原生下拉刷新

先看效果,相比舊版 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 同時支援雙向操作

目前較成熟的選擇是 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 上拉載入,避免使用者拉到空轉。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved