今天碰到界面需要刷新的功能,于是找到了androidx.swiperefreshlayout 控件
使用步骤:
1、引用androidx.swiperefreshlayout 包,在app目录下的build.gradle 添加如下:
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
2、在xml文件添加
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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:id="@+id/swipeRedreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="TextView"
android:textSize="30sp" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
3、activity使用
private SwipeRefreshLayout swipeRefreshLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// getAd();
mContext = getContext();
view = inflater.inflate(R.layout.fragment_con, container, false);
swipeRefreshLayout = view.findViewById(R.id.sw_refresh);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
searchWifi();
}
});
//取消刷新旋转图标
swipeRefreshLayout.setRefreshing(false);
return view;
}
参考文档: