Android基礎SwipeRefreshLayout的使用

SwipeRefreshLayout簡介:

google官方推出的下拉刷新組件SwipeRefreshLayout,被放到support v4中。
其實通過文檔我們可以知道SwipeRefreshLayout只不過是繼承了ViewGroup。
查看文檔,我們可以知道,在SwipRefreshLayout中存在一個接口,通過此接口我們可以監聽滑動手勢,其實使用此組件最重要的步驟就是實現此接口的onRefresh方法,在此方法中實現數據的更新操作。

查看源碼:

android studio 查看源碼,選擇類,按住ctrl

public class SwipeRefreshLayout extends ViewGroup implements NestedScrollingParent,
        NestedScrollingChild {
        ```
        ```
        }

SwipeRefreshLayout裏面需要注意的API:

1、setOnRefreshListener(OnRefreshListener listener)
設置下拉監聽,當用戶下拉的時候會去執行回調 2、setColorSchemeColors(int… colors)
(過期的方法)設置 進度條的顏色變化,最多可以設置4種顏色 2、setColorSchemeResources(int…
colorResIds):設置進度動畫的顏色。 3、setProgressViewOffset(boolean scale, int
start, int end) 調整進度條距離屏幕頂部的距離 4、setRefreshing(boolean refreshing)
設置SwipeRefreshLayout當前是否處於刷新狀態,一般是在請求數據的時候設置爲true,在數據被加載到View中後,設置爲false。
5、setSize(int size):設置進度圈的大小,只有兩個值:DEFAULT、LARGE
6、setProgressBackgroundColor(int colorRes):設置進度圈的背景色。

使用步驟:

佈局調用:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingTop="10dp"
            android:text="刷新"
            android:textSize="20sp"
            android:textStyle="bold" />
    </ScrollView>

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

Activity調用:

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.textView1);
        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container);
        //設置刷新時動畫的顏色,可以設置4個
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                tv.setText("正在刷新");
                // TODO Auto-generated method stub
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        tv.setText("刷新完成");
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 6000);
            }
        });
    }
}

測試:

這裏寫圖片描述

看效果出來了,就是這樣簡單。

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