SwipeRefreshLayout是官方提供的下拉刷新解决控件,具有使用简单,灵活等特点。
Android4.0以下的版本需要用到Android-support-v4.jar包才能用到
Android-support-v4.jar包下载地址:https://download.youkuaiyun.com/download/qq_17798399/11261669
基本使用
在xml添加引入SwipeRefreshLayout的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
在Activity中使用SwipeRefreshLayout
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//模拟网络请求需要3000毫秒,请求完成,设置setRefreshing 为false
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
}, 3000);
}
});
主要方法
(1)setOnRefreshListener(OnRefreshListener): 为布局添加一个Listener
(2)setRefreshing(boolean): 显示或隐藏刷新进度条
(3)isRefreshing(): 检查是否处于刷新状态
(4)setColorSchemeResources(): 设置进度条的颜色主题,最多能设置四种
进阶
SwipeRefreshLayout 还提供了一些方法设置进度的样式,滑动的开始位置和结束位置等
方法名 | 作用 |
---|---|
setSize(int size) | 设置进度View样式的大小,只有两个值DEFAULT和LARGE |
setProgressViewOffset(boolean scale, int start, int end) | 设置进度View下拉的起始点和结束点,scale 是指设置是否需要放大或者缩小动画 |
setProgressViewEndTarget(boolean scale, int end) | 设置进度View下拉的结束点,scale 是指设置是否需要放大或者缩小动画 |
setDistanceToTriggerSync(int distance) | 设置触发刷新的距离 |
setOnChildScrollUpCallback(@Nullable OnChildScrollUpCallback callback)k | 如果child是自己自定义的view,可以通过这个回调,告诉swipeRefreshLayoutchild是否可以滑动 |