一个体验极好的轻量级下拉刷新、上拉加载更多的LightRefresh库
LightRefresh特点(轻量级!轻量级!轻量级!方便定制你自己的专属刷新控件。整个LightRefresh大概只有10个类(用大类裤?这辈子都不可能用大类裤))
代码结构
- 1.可以不要刷新头(支持拉动回弹效果,类似于qq的下拉刷新,体验非常好)
- 2.支持常用的控件(RecyclerView、ListView、WebView、ScrollView等等).
- 3.支持自定义事件分发和冲突处理(将处理事件冲突的方法回调出来方便用户自己定义).
- 4.支持自定义下拉刷新头和加载更多头.
- 5.支持多指触控,不会让整个布局抖动,这是目前大部分下拉刷新库都没有注意到的.
- 6.支持自动刷新.
- 7.支持固定不回弹布局(即将舍去:该刷新库目的是打造回弹效果的刷新库,将不再支持固定布局效果).
效果图
如何使用
1.添加依赖:
在根 build.gradle 中添加maven:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
然后添加:
dependencies {
implementation 'com.github.BLCodes:LightRefresh:1.0.3-beta-6'
}
2.用一个framlayout(必须!!!) 包裹BounceLayout 和header (footer),比如:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl_root"
>
<!--the header or footer will be added here-->
<com.blcodes.views.refresh.BounceLayout
android:id="@+id/bl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_test"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</com.blcodes.views.refresh.BounceLayout>
</FrameLayout>
3.添加刷新头、上拉加载更过头(如果你不想让布局有回弹效果,可以使用setDisallowBounce这个方法):
//bounceLayout.setDisallowBounce(true);//disallow the BounceLayout bounce!!!!
FrameLayout rootView = findViewById(R.id.fl_root);
bounceLayout.setHeaderView(new DefaultHeader(this),rootView);//if HeaderView is null,it just bounce.
bounceLayout.setFooterView(new DefaultFooter(this),rootView);
4.设置事件分发和冲突处理回调,注意:一般情况下只有竖直方向有滚动直接在notForwarding方法中返回true,如果既有竖直方向的滚动又有水平方向的滚动,可以参照demo中的处理
RecyclerView recyclerView = findViewById(R.id.rv_test);
bounceLayout.setBounceHandler(new NormalBounceHandler(),recyclerView);
bounceLayout.setEventForwardingHelper(new EventForwardingHelper() {
@Override
public boolean notForwarding(float downX, float downY, float moveX, float moveY) {
return true;
}
});
比如上面的侧拉删除和Recyclerview的下拉刷新,我们希望在下拉的时候能拉出刷新头,又能够在左右滑动的时候进行侧拉删除,LightRefresh库通过接口回调的方式可以让用户方便的定制,而不是改源码的方式,那样非常麻烦,笔者之前用过的一些刷新库都是改分发的事件方法,那样的话极不方便:
bounceLayout.setDispatchAble(false);//设置可以分发给事件给子view,因为move事件刚开始x的方向变化和y方向是差不多的,所以只能在后续的move中确定是下拉还是左右拉。
bounceLayout.setEventForwardingHelper((downX, downY, moveX, moveY) -> {
View view = rvAddress.findChildViewUnder(downX,downY);
if (view!=null) {
if (Math.abs(view.getScrollX()) > 0) {
bounceLayout.setRefreshCompleted();//这样会初始化bounceLayout
return false;//侧拉
}
}
float distanceX = Math.abs(moveX - downX);
float distanceY = Math.abs(moveY - downY);
return !(distanceX >= distanceY);//进行判断
});
经过以上操作之后将会很流畅的进行侧拉或者下拉。
5.添加下拉刷新和上拉加载的回调: 设置bounceLayout.setRefreshCompleted()
和bounceLayout.setLoadingMoreCompleted()
这一步非常重要,不要忘记了
bounceLayout.setBounceCallBack(new BounceCallBack() {
@Override
public void startRefresh() {
Log.i(TAG, "run: 开始刷新");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ArrayList<String> data = new ArrayList<>();
for (int i = 0; i < 16; i++) {
data.add("新文本"+i);
}
adapter.setData(data);
bounceLayout.setRefreshCompleted();
Log.i(TAG, "run: 结束刷新");
}
},2000);
}
@Override
public void startLoadingMore() {
}
});
6.如果你需要自动刷新,可以设置:
bounceLayout.autoRefresh()
7.自定义刷新头
第一步,继承抽象类BaseHeaderView;
第二步,处理BounceLayout的回弹方法:
/**
* @param v 整个布局的移动距离
*/
@Override
public void handleDrag(float v) {
this.totalOffset = v;
if (canTranslation) {
setTranslationY(v);
}
if (status == HEADER_REFRESHING) {//只要是正在刷新
return;
}
if (v <= 0) {//回到初始位置
status = HEADER_DRAG;
tvHeaderTip.setText(mContext.getResources().getString(R.string.refresh_drag));
stopFrameAnim();
}
if (status == HEADER_DRAG) {//开始拖动
if (v >= childHeight) {//一旦超过刷新头高度
status = HEADER_RELEASE;
tvHeaderTip.setText(mContext.getResources().getString(R.string.refresh_release));
stopFrameAnim();
}
}
if (status == HEADER_RELEASE) {//还未释放拖拉回去
if (v <= childHeight) {//一旦低于刷新头高度
status = HEADER_DRAG;
tvHeaderTip.setText(mContext.getResources().getString(R.string.refresh_drag));
stopFrameAnim();
}
}
}
第三步,设置到布局中去:
bounceLayout.setHeaderView(new FrameRefreshHeader(this), flRoot);
完整类(效果如上自定义刷新头):
public class FrameRefreshHeader extends BaseHeaderView {
/*刷新头的状态*/
private int status;
/*头布局高度*/
private int childHeight;
/*布局偏移量*/
private float totalOffset;
private Context mContext;
private AnimationDrawable frameAnimation;
/*-----------刷新头布局视图内容---------------*/
private TextView tvHeaderTip;
private ImageView ivHeaderTip;
public FrameRefreshHeader(Context context) {
this(context, null);
}
public FrameRefreshHeader(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public FrameRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
setBackgroundColor(Color.parseColor("#ffffff"));
mContext = context;
status = HEADER_DRAG;
childHeight = mContext.getResources().getDimensionPixelSize(R.dimen.frame_header_height);
//添加头内容
View view = LayoutInflater.from(context).inflate(R.layout.header_frame_loading, this, false);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
, childHeight);
addView(view, lp);
tvHeaderTip = (TextView) view.findViewById(R.id.tv_tip);
ivHeaderTip = (ImageView) view.findViewById(R.id.iv_tip);
setXml2FrameAnim();
}
/**
* @param v 整个布局的移动距离
*/
@Override
public void handleDrag(float v) {
this.totalOffset = v;
if (canTranslation) {
setTranslationY(v);
}
if (status == HEADER_REFRESHING) {//只要是正在刷新
return;
}
if (v <= 0) {//回到初始位置
status = HEADER_DRAG;
tvHeaderTip.setText(mContext.getResources().getString(R.string.refresh_drag));
stopFrameAnim();
}
if (status == HEADER_DRAG) {//开始拖动
if (v >= childHeight) {//一旦超过刷新头高度
status = HEADER_RELEASE;
tvHeaderTip.setText(mContext.getResources().getString(R.string.refresh_release));
stopFrameAnim();
}
}
if (status == HEADER_RELEASE) {//还未释放拖拉回去
if (v <= childHeight) {//一旦低于刷新头高度
status = HEADER_DRAG;
tvHeaderTip.setText(mContext.getResources().getString(R.string.refresh_drag));
stopFrameAnim();
}
}
}
@Override
public boolean doRefresh() {
if (status == HEADER_REFRESHING && totalOffset == childHeight) {//正在刷新,并且偏移量==刷新头高度才认为刷新
return true;
}
return false;
}
@Override
public void setParent(ViewGroup viewGroup) {
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
,ViewGroup.LayoutParams.WRAP_CONTENT);
lp.topMargin = - childHeight;
viewGroup.addView(this,lp);
}
@Override
public boolean checkRefresh() {
if ((status == HEADER_RELEASE || status == HEADER_REFRESHING) && totalOffset >= childHeight) {
status = HEADER_REFRESHING;
tvHeaderTip.setText(mContext.getResources().getString(R.string.refreshing_custom));
startFrameAnim();
return true;
} else {
return false;
}
}
@Override
public void refreshCompleted() {
status = HEADER_COMPLETED;
tvHeaderTip.setText(mContext.getResources().getString(R.string.refresh_completed));
stopFrameAnim();
}
@Override
public int getHeaderHeight() {
return childHeight;
}
@Override
public void autoRefresh() {
status = HEADER_REFRESHING;
tvHeaderTip.setText(mContext.getResources().getString(R.string.refreshing_custom));
startFrameAnim();
}
/**
* 通过XML添加帧动画
*/
private void setXml2FrameAnim() {
// 通过逐帧动画的资源文件获得AnimationDrawable示例
frameAnimation = (AnimationDrawable) getResources().getDrawable(
R.drawable.loading_frame);
ivHeaderTip.setBackground(frameAnimation);
}
private void startFrameAnim(){
if (frameAnimation != null && !frameAnimation.isRunning()) {
frameAnimation.start();
}
}
private void stopFrameAnim(){
if (frameAnimation != null && frameAnimation.isRunning()) {
frameAnimation.selectDrawable(0);
frameAnimation.stop();
}
}
}
经正式上线项目测试,该库使用起来效果非常好,特别是在多指操作和回弹效果上,并且耦合性也比较低,如果有问题,可以给他们的开源团队提问,该库也在一直维护中,地址为:https://github.com/tangxianqiang/LightRefresh.