SwipeRefreshLayout是一个Google官方提供的下拉刷新控件,通过下拉刷新可以及时更新数据,提高用户体验。它主要有两个特点:
- 支持多种样式的下拉刷新
- 支持多种手势操作:下拉刷新、上拉加载更多、左滑、右滑
一、基本用法
1、首先在build.gradle中引入SwipeRefreshLayout库:
dependencies {
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
}
2、在布局文件中添加SwipeRefreshLayout控件,注意SwipeRefreshLayout只能有一个子控件:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
3、在Java代码中获取SwipeRefreshLayout控件,并设置刷新监听器:
SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//下拉刷新操作
}
});
4、在下拉刷新操作完成后,记得调用SwipeRefreshLayout的setRefreshing(false)方法,取消刷新状态:
swipeRefreshLayout.setRefreshing(false);
二、自定义样式
SwipeRefreshLayout提供了两种样式的刷新效果:传统样式和Material Design样式。我们可以通过setProgressBackgroundColorSchemeResource()和setColorSchemeResources()方法来改变刷新样式,也可以自定义实现更加炫酷的自己的刷新样式。
1、传统样式刷新:
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
2、Material Design样式刷新:
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
3、自定义样式刷新:
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorPrimary);
swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
swipeRefreshLayout.setProgressViewOffset(true, -100, 300);
//设置刷新视图
CustomRefreshView customRefreshView = new CustomRefreshView(context);
swipeRefreshLayout.addView(customRefreshView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
class CustomRefreshView extends View implements SwipeRefreshLayout.OnChildScrollUpCallback {
Paint mPaint;
int mOffsetTop = 0;
public CustomRefreshView(Context context) {
super(context);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0, 0 + mOffsetTop, getWidth(), getHeight() + mOffsetTop, mPaint);
}
//OnChildScrollUpCallback回调方法,返回值决定是否可以下拉刷新
@Override
public boolean canChildScrollUp(SwipeRefreshLayout parent, @Nullable View child) {
return false;
}
public void setOffsetTop(int offsetTop) {
mOffsetTop = offsetTop;
invalidate();
}
}
//刷新操作完成后调用,注意中间需要延时,确保CustomRefreshView滑动到顶部
ObjectAnimator animator = ObjectAnimator.ofInt(customRefreshView, "offsetTop", customRefreshView.mOffsetTop, 0);
animator.setDuration(300);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
animator.start();
}
}, 1000);
三、手势操作
SwipeRefreshLayout除了支持下拉刷新外,还支持了上拉加载更多和左右滑动手势,我们可以通过setOnTouchListener()方法来监听触摸事件,并根据手势来进行逻辑处理。
1、上拉加载更多:
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setOnTouchListener(new OnSwipeTouchListener(this){
@Override
public void onBottom() {
//上拉加载更多操作
}
});
abstract class OnSwipeTouchListener implements OnTouchListener {
private GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onLeftSwipe() {
}
public void onRightSwipe() {
}
public void onTop() {
}
public void onBottom() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onRightSwipe();
} else {
onLeftSwipe();
}
result = true;
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY < 0) {
onTop();
} else {
onBottom();
}
result = true;
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
}
2、左右滑动:
recyclerView.setOnTouchListener(new OnSwipeTouchListener(this){
@Override
public void onLeftSwipe() {
//左滑操作
}
@Override
public void onRightSwipe() {
//右滑操作
}
});
四、小结
通过本文,我们详细了解了SwipeRefreshLayout的基本用法、颜色样式以及手势操作。SwipeRefreshLayout作为Android平台上常用的下拉刷新控件,具有良好的用户交互体验,对于提升应用质量具有重要意义。