1. 程式人生 > >SwipeRefreshLayout安卓最普通的下拉重新整理

SwipeRefreshLayout安卓最普通的下拉重新整理

這就是這個系統的效果:

這裡寫圖片描述
記得好似是半年前或者是更久,我看到了一個下拉重新整理的博文,感覺這個小圓圈的載入效果很棒,於是去看看是如何實現的是shape還是圖片,還是自定view自己畫的啊。結果找了半天未果。後來有一段時間才搞清楚,原來是系統自帶的SwipeRefreshLayout在5.0新的顯示效果。哦哦,讓我誇(黑)一發,安卓也有系統控制元件直接拿出來就很不錯的了。MD設計萬歲。算了不扯了,接下來介紹一下這種很簡單的系統下拉重新整理的使用方式吧。
首先這貨是實現NestedScrollingChild和NestedScrollingParent兩個介面的Viewgroup,裡面放ScrollView RecyclerView listview都可以的。
xml中這樣用就可以了:

<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_container"
                                              xmlns:android="http://schemas.android.com/apk/res/android"
                                              android:layout_width="match_parent"
                                              android:layout_height
="match_parent">
<ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#d2d284"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation
="vertical" >
<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"/> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:onClick="re" android:paddingTop="10dp" android:text="下拉重新整理" android:textSize="20sp" android:textStyle="bold"/> <ImageView android:layout_width="match_parent" android:layout_height="900dp" android:scaleType="fitXY" android:src="@mipmap/ic_launcher"/> </LinearLayout> </ScrollView> </android.support.v4.widget.SwipeRefreshLayout>

然後find各個控制元件,並寫一個介面模擬一下耗時任務:

SwipeRefreshLayout.OnRefreshListener listener;

listener = new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {

                mTextView.setText("正在重新整理");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        mTextView.setText("重新整理完成");
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 3000);
            }
        };

如果想不下拉直接觸發載入中狀態:
private void doReF() {

    swipeRefreshLayout.setRefreshing(true);
    listener.onRefresh();
}

如果在onCreat中調動一定加在post裡:

private void doReF() {

        swipeRefreshLayout.setRefreshing(true);
        listener.onRefresh();
    }

一般的呼叫:

private void doReF() {

        swipeRefreshLayout.setRefreshing(true);
        listener.onRefresh();
    }

例子比較簡單,筆者也是回味了一下安卓開發路上的小小波瀾。

歡迎關注作者。歡迎評論討論。歡迎拍磚。 如果覺得這篇文章對你有幫助,歡迎打賞, 歡迎star,Fork我的github。 喜歡作者的也可以Follow。也算對作者的一種支援。 本文Github程式碼連結

歡迎加作者自營安卓開發交流群:308372687

這裡寫圖片描述