1. 程式人生 > >WaveSwipeRefreshLayout +RecyclerView 實現簡單的水滴下拉重新整理

WaveSwipeRefreshLayout +RecyclerView 實現簡單的水滴下拉重新整理

第一步:
1.在app build.gradle中加入依賴:

           implementation  'com.github.recruit-lifestyle:WaveSwipeRefreshLayout:1.6'

2.AndroidManifest中新增網路許可權

         <uses-permission android:name="android.permission.INTERNET"/>
         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

第二步:main.xml佈局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.MainActivity">
     <LinearLayout
        android:id="@+id/ss"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="下載重新整理"
            android:paddingBottom="10dp"
            android:paddingTop="5dp"
            android:gravity="center"
            android:textColor="@color/text_font_white"
            android:textSize="18sp" />
      </LinearLayout>
            <ListView
                android:id="@+id/list_real"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/ss"
                android:background="#FCFCFC" />

            <jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout
                android:layout_below="@+id/ss"
                android:id="@+id/swipe_refresh_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout>

        </RelativeLayout>

第三步:MainActivity中

     private RecyclerView recyclerView;
    private WaveSwipeRefreshLayout mWaveSwipeRefreshLayout;
    private Handler handler = new Handler();

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
         //找到重新整理佈局
        swipeRefreshLayout=(WaveSwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        refreshMethod();//下拉重新整理具體方法
        }

     //---------------下拉重新整理具體方法--------------
    private void refreshMethod(){
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        ////瀑布流佈局
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        //給recyclerView設定為瀑布流佈局
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        //設定圓圈顏色
        swipeRefreshLayout.setColorSchemeColors(Color.BLACK,Color.WHITE);
        //整體顏色
        swipeRefreshLayout.setWaveColor(Color.argb(238, 87,250,255));
        //設定下拉監聽器
        swipeRefreshLayout.setOnRefreshListener(new WaveSwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshFruits();//這個方法為具體的重新整理網路請求步驟,
            }
        });
    }
//這裡,模擬網路請求
private void  refreshFruits() {
handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       
                        //三秒後停止重新整理
                        mWaveSwipeRefreshLayout.setRefreshing(false);
                    }
                },3000);
}``

最後:  swipeRefreshLayout.setRefreshing(false);隱藏重新整理進度圖