Android學習--SwipeRefreshLayout

 下拉刷新是我們在瀏覽APP的時候使用非常平凡的功能,目前有多重比較優秀的刷新控件。其中的     SwipeRefreshLayout是Google自己在V4包中一個下拉刷新控件,簡單易用。

     控件的引用

      import android.support.v4.widget.SwipeRefreshLayout;

      控件效果

      

   下面我們簡述一下使用方式,比較簡單

    1)在XML佈局文件中引用SwipeRefreshLayout,只能包含一個子控件

 <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </ListView>
    </android.support.v4.widget.SwipeRefreshLayout>

    2)在Activity中獲取並綁定監聽

 swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);

        listView = (ListView) findViewById(R.id.listview);
        //設置刷新監聽器
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            //實現刷新時間
            @Override
            public void onRefresh() {
                Toast.makeText(MainActivity.this, "正在刷新數據", Toast.LENGTH_LONG).show();
                //do something
                //........
                //設置刷新狀態爲取消狀態,即完成刷新
                swipeRefreshLayout.setRefreshing(false);
            }
        });

     其中 OnRefreshListener是SwipeRefreshLayout的刷新監聽器,需要重寫其中的onRefresh()方法。

     我們的數據刷新邏輯就放在onRefresh()中

     其中的 swipeRefreshLayout.setRefreshing(false);設置刷新狀態,當我們處理完數據的時候需要結束刷新,否則頁面將始終處於刷新的狀態

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章