第一步:
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);隐藏刷新进度图