public class CustomRefreshHeader extends LinearLayout implements RefreshHeader {
private ImageView mImage;
private AnimationDrawable pullDownAnim;
private AnimationDrawable refreshingAnim;
private TextView tv_refresh_header;
private boolean hasSetPullDownAnim = false;
public CustomRefreshHeader(Context context) {
this(context, null, 0);
}
public CustomRefreshHeader(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.refreshheader, this);
mImage = (ImageView) view.findViewById(R.id.header_iv);
tv_refresh_header = (TextView) view.findViewById(R.id.header_tv);
}
@NonNull
@Override
public View getView() {
return this;
}
@Override
public SpinnerStyle getSpinnerStyle() {
return SpinnerStyle.Translate;
}
@Override
public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {
}
/**
* 状态改变时调用。在这里切换第三阶段的动画卖萌小人
* @param refreshLayout
* @param oldState
* @param newState
*/
@Override
public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
switch (newState) {
case PullDownToRefresh: //下拉刷新开始。正在下拉还没松手时调用
mImage.setImageResource(R.mipmap.image);
tv_refresh_header.setText("下拉可以刷新");
break;
case Refreshing: //正在刷新。只调用一次
mImage.setImageResource(R.drawable.refresh);
refreshingAnim = (AnimationDrawable) mImage.getDrawable();
tv_refresh_header.setText("正在刷新...");
refreshingAnim.start();
break;
case ReleaseToRefresh:
break;
}
}
/**
* 下拉过程中不断调用此方法。第一阶段从小变大的小人头动画,和第二阶段翻跟头动画都在这里设置
*/
@Override
public void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {
// 下拉的百分比小于100%时,不断调用 setScale 方法改变图片大小
if (percent < 1) {
mImage.setScaleX(percent);
mImage.setScaleY(percent);
//是否执行过翻跟头动画的标记
if (hasSetPullDownAnim) {
hasSetPullDownAnim = false;
}
}
//当下拉的高度达到Header高度100%时,开始加载正在下拉的初始动画,即翻跟头
if (percent >= 1.0) {
if (!hasSetPullDownAnim) {
tv_refresh_header.setText("松开立即刷新...");
mImage.setImageResource(R.mipmap.image2);
}
}
}
/**
* 动画结束后调用
*/
@Override
public int onFinish(RefreshLayout layout, boolean success) {
// 结束动画
if (pullDownAnim != null && pullDownAnim.isRunning()) {
pullDownAnim.stop();
}
if (refreshingAnim != null && refreshingAnim.isRunning()) {
refreshingAnim.stop();
}
//重置状态
hasSetPullDownAnim = false;
return 0;
}
@Override
public void onReleasing(float percent, int offset, int headerHeight, int extendHeight) {
}
@Override
public void onRefreshReleased(RefreshLayout layout, int headerHeight, int extendHeight) {
}
@Override
public void setPrimaryColors(int... colors) {
}
@Override
public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {
}
@Override
public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
}
@Override
public boolean isSupportHorizontalDrag() {
return false;
}
}