CoolRefreshView
1. 支持任意View的刷新
2. 支持自定义Header
3. 支持NestedScrollingParent,NestedScrollingChild的事件分发
4. 嵌套ViewPager不会有事件冲突
7.导入方式
<1>gradle导入
compile 'com.shizhefei:CoolRefreshView:1.0.1'
由于用到了v4和recyclerview所以也要导入他们
compile 'com.android.support:support-v4:23.4.0'
<2>jar包方式导入
Download sample Apk
8.历史版本和更新信息
https://github.com/LuckyJayce/CoolRefreshView/releases
效果图
使用方法
xml
<com.shizhefei.view.coolrefreshview.CoolRefreshView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.shizhefei.view.coolrefreshview.CoolRefreshView>
代码
coolRefreshView.setPullHeader(new MaterialHeader(getContext()));
//添加刷新监听
coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
@Override
public void onRefreshing(CoolRefreshView refreshView) {
}
});
其它可用方法
设置全局的IPullHeaderFactory
//静态设置全局的IPullHeaderFactory,一次配置,所有默认使用这个factory生成的PullHeader
//就不用每个界面设置,该代码可以放在Application onCreate
CoolRefreshView.setPullHeaderFactory(new IPullHeaderFactory() {
@Override
public PullHeader made(Context context) {
JellyHeader jellyHeader = new JellyHeader(getContext());
jellyHeader.setDragLayoutColor(ContextCompat.getColor(getContext(), R.color.primary));
jellyHeader.setLoadingView(R.layout.layout_jelley_header);
return jellyHeader;
}
@Override
public boolean isPinContent() {
return false;
}
});
设置ContentView是否和头部一起下拉
/**
* @param pullHeader 刷新的头部
* @param isPinContent true滚定内容不下拉,只下拉头部, false 一起下拉
*/
setPullHeader(PullHeader pullHeader, boolean isPinContent)
自定义Header
<1>简单的实现
实现PullHeader
public class TestHeader implements PullHeader {
private View headView;
private TextView mTextView;
@Override
public View createHeaderView(final CoolRefreshView refreshView) {
headView = LayoutInflater.from(refreshView.getContext()).inflate(R.layout.coolrecyclerview_testhead, refreshView, false);
mTextView = (TextView) headView.findViewById(R.id.coolrecyclerview_testhead_text1_textView);
return headView;
}
/**
* 开始拖动
*/
@Override
public void onPullBegin(CoolRefreshView refreshView) {
isDownArrow = true;
}
/**
* 位置变化
*
* @param refreshView
* @param status 状态 /没有任何操作
* public final static byte PULL_STATUS_INIT = 1;
* //开始下拉
* public final static byte PULL_STATUS_TOUCH_MOVE = 2;
* //回到原始位置
* public final static byte PULL_STATUS_RESET = 3;
* //刷新中
* public final static byte PULL_STATUS_REFRESHING = 4;
* //刷新完成
* public final static byte PULL_STATUS_COMPLETE = 5;
* @param dy 下拉事件的位移
* @param currentDistance 当前位移的距离
*/
@Override
public void onPositionChange(CoolRefreshView refreshView, int status, int dy, int currentDistance) {
int offsetToRefresh = getConfig().offsetToRefresh(refreshView, headView);
if (status == CoolRefreshView.PULL_STATUS_TOUCH_MOVE) {
if (currentDistance < offsetToRefresh) {
if (!isDownArrow) {
mTextView.setText("下拉刷新");
isDownArrow = true;
}
} else {
if (isDownArrow) {
mTextView.setText("释放立即刷新");
isDownArrow = false;
}
}
}
}
private boolean isDownArrow;
/**
* 刷新中
*/
@Override
public void onRefreshing(CoolRefreshView refreshView) {
mTextView.setText("刷新中..");
}
/**
* 没有刷新的释放回去
*/
@Override
public void onReset(CoolRefreshView refreshView, boolean pullRelease) {
}
/**
* 设置刷新完成,并且释放回去
*/
@Override
public void onPullRefreshComplete(CoolRefreshView frame) {
}
@Override
public Config getConfig() {
return config;
}
//使用默认的配置
private DefaultConfig config = new DefaultConfig();
}
也可以直接继承View 实现 PullHeader
public class TestHeader extends View implements PullHeader {
@Override
public View createHeaderView(final CoolRefreshView refreshView) {
return this;
}
}
<2>配置距离,可以实现更丰富的效果
abstract class Config {
/**
* 超出这个偏移量,松开手指就会触发刷新。
*/
public abstract int offsetToRefresh(CoolRefreshView refreshView, View headerView);
/**
* 显示刷新的位置的偏移量
*/
public abstract int offsetToKeepHeaderWhileLoading(CoolRefreshView refreshView, View headerView);
/**
* 刷新控件总共可以下拉拖动的距离
*/
public abstract int totalDistance(CoolRefreshView refreshView, View headerView);
/**
* headView 在布局中的偏移量
*/
public abstract int headerViewLayoutOffset(CoolRefreshView refreshView, View headerView);
/**
* contentView 是否可以向上滚动,用来判断是否可以下拉刷新,如果可以向上滚动就不做下拉刷新动作
*/
public abstract boolean contentCanScrollUp(CoolRefreshView refreshView, View contentView);
/**
* 拦截滑动事件
*
* @param refreshView
* @param dy 触摸滑动的偏移量
* @param currentDistance 当前的滑动的距离
* @param totalDistance 总的可下拉距离
* @return
*/
public abstract int dispatchTouchMove(CoolRefreshView refreshView, int dy, int currentDistance, int totalDistance);
}
默认的配置
//滑动的总距离是header高度3倍
//触发刷新的距离是header的1.2倍
//正在刷新中的距离是header的高度
class DefaultConfig extends Config {
@Override
public int offsetToRefresh(CoolRefreshView refreshView, View headerView) {
return (int) (headerView.getMeasuredHeight() * 1.2f);
}
@Override
public int offsetToKeepHeaderWhileLoading(CoolRefreshView refreshView, View headerView) {
return headerView.getMeasuredHeight();
}
@Override
public int totalDistance(CoolRefreshView refreshView, View headerView) {
return headerView.getMeasuredHeight() * 3;
}
.....
}
第二种常用的配置
//总的滑动是header的高度,也就是滑动显示的区域控制在header里
//正在刷新的距离是header3分之一,在header底部的3分之一区域作为正在刷新中的区域
//触发刷新的距离是正在刷新距离的1.2倍.
private DefaultConfig config = new DefaultConfig() {
@Override
public int offsetToRefresh(CoolRefreshView refreshView, View headerView) {
return (int) (headerView.getMeasuredHeight() / 3 * 1.2f);
}
@Override
public int offsetToKeepHeaderWhileLoading(CoolRefreshView refreshView, View headerView) {
return headerView.getMeasuredHeight() / 3;
}
@Override
public int totalDistance(CoolRefreshView refreshView, View headerView) {
return headerView.getMeasuredHeight();
}
};
配合MVCHelper类库使用,上滑加载更多
配合 https://github.com/LuckyJayce/MVCHelper
代码只要5行,就可以实现下拉刷新,滚动底部自动加载更多,分页加载,自动切换显示网络失败布局,暂无数据布局
CoolRefreshView coolRefreshView = (CoolRefreshView) findViewById(R.id.coolRefreshView);
MVCHelper<List<Book>> mvcHelper = new MVCCoolHelper<List<Book>>(coolRefreshView);
// 设置数据源
mvcHelper.setDataSource(new BooksDataSource());
// 设置适配器
mvcHelper.setAdapter(new BooksAdapter(this));
// 加载数据
mvcHelper.refresh();