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

下拉刷新不用愁:SwipeRefreshLayout 使用要點

編輯:關於 Android 程式設計

SwipeRefreshLayout 是官方提供的下拉刷新控件,用法極簡,但有幾個硬性約束需要記住。

唯一的硬性限制

SwipeRefreshLayout 下面只能有一個直接子節點。如果內容需要多個 View,必須先包一層 FrameLayout 或 LinearLayout,否則運行時直接崩潰。

布局文件

<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" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</FrameLayout>

注意這裡容器是 FrameLayout,SwipeRefreshLayout 是在代碼中動態包裹或直接作為容器替換的——示例中把 findViewById(R.id.swipe_container) 對應的容器聲明為 SwipeRefreshLayout,其子節點只有一個 ListView。

核心 API

  • setOnRefreshListener(OnRefreshListener):註冊下拉手勢的監聽;
  • setRefreshing(boolean):手動顯示或隱藏刷新進度條;
  • isRefreshing():查詢當前是否處於刷新狀態;
  • setColorSchemeResource(...):設置進度條配色,最多支持四種。

主程序

public class MyActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout mSwipeLayout;
    private ListView mListView;
    private ArrayAdapter mArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        String[] strings = new String[]{"a", "b", "c", "d"};
        mArrayAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, strings);

        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        mSwipeLayout.setOnRefreshListener(this);

        // 加載動畫的顏色是循環播放的,只要刷新未完成就會一直循環
        // color1 > color2 > color3 > color4
        mSwipeLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        mListView = (ListView) findViewById(R.id.list);
        mListView.setAdapter(mArrayAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_grid_view) {
            startActivity(new Intent(MyActivity.this, GridViewTest.class));
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeLayout.setRefreshing(false);   // 停止刷新
            }
        }, 3000);
    }
}

實踐要點

  • 刷新結束一定要調 setRefreshing(false),否則進度條會一直轉,手勢也被鎖住。
  • 進度條只在下拉超過閾值時出現,閾值可通過 setDistanceToTriggerSync() 調整。
  • 與 RecyclerView 協同時,若列表本身可以滾動,通常沒有衝突;但內部嵌了横向 ViewPager 或 WebView 時,可能出現手勢搶佔,需要自定義 shouldInterceptTouchEvent 邏輯或用 setEnabled(false) 臨時禁用。
  • 示例用 Handler 延遲 3 秒模擬網絡請求,實戰中應把 setRefreshing(false) 放在請求成功的回調裡,並在失敗分支也調一次(或放 finally)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved