CommonRefreshHeader.java 6.54 KB
package com.wd.common.widget;


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.airbnb.lottie.LottieAnimationView;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.simple.SimpleComponent;
import com.scwang.smart.refresh.layout.util.SmartUtil;
import com.wd.foundation.wdkit.utils.AnimUtil;
import com.wd.fastcoding.base.R;

/**
 * 下拉刷新头部
 */
public class CommonRefreshHeader extends SimpleComponent implements RefreshHeader {

    private LottieAnimationView animationView;
    private LottieAnimationView animationView2;
    private RefreshState newState;
    private TextView tvDesc;
    View view;

    /**
     * 下拉刷新 不使用刷新头动效
     */
    private boolean isTransparent;

    // 动效
    private String EffectStep1 = "step1.json";
    private String EffectStep2 = "step2.json";

    public CommonRefreshHeader(Context context) {
        this(context, null);
    }

    public CommonRefreshHeader(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        view = View.inflate(context, R.layout.refresh_header, this);
        animationView = view.findViewById(R.id.animation_view);
        animationView2 = view.findViewById(R.id.animation_view2);
        animationView2.setVisibility(GONE);
        tvDesc = view.findViewById(R.id.tv_desc);
        tvDesc.setVisibility(GONE);
        //tvDesc.setTextColor(ContextCompat.getColor(AppContext.getContext(), R.color.res_color_general_bbbbbb));
    }

    /**
     * 设置白色动效资源
     */
    public void setWhiteEffectSource() {
        EffectStep1 = "refresh_step1_white.json";
        EffectStep2 = "refresh_step2_white.json";
      //  tvDesc.setTextColor(ContextCompat.getColor(AppContext.getContext(), R.color.res_color_common_C8));
    }

    /**
     * 获取真实视图(必须返回,不能为null)一般就是返回当前自定义的view
     */
    @NonNull
    @Override
    public View getView() {
        return this;
    }


    /**
     * 获取变换方式(必须指定一个:平移、拉伸、固定、全屏),Translate指平移,大多数都是平移
     */
    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }


    /**
     * 手指拖动下拉(会连续多次调用,用于实时控制动画关键帧)
     *
     * @param percent       下拉的百分比 值 = offset/headerHeight (0 - percent - (headerHeight+maxDragHeight) / headerHeight )
     * @param offset        下拉的像素偏移量  0 - offset - (headerHeight+maxDragHeight)
     * @param height        Header的高度
     * @param maxDragHeight 最大拖动高度
     */
    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
        if (newState == RefreshState.PullDownToRefresh) {
            animationView.setProgress(percent);
        }

    }

    /**
     * 一般可以理解为一下case中的三种状态,在达到相应状态时候开始改变
     * 注意:这三种状态都是初始化的状态
     */
    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        this.newState = newState;
        switch (newState) {
            // 下拉刷新的开始状态:下拉可以刷新
            case PullDownToRefresh:
                tvDesc.setVisibility(GONE);
                animationView2.setVisibility(GONE);
                // 刷新动画透明
                if (isTransparent()) {
                    animationView.setVisibility(INVISIBLE);
                } else {
                    AnimUtil.showLocalLottieEffects(animationView, EffectStep1, false);
                }
                break;
            // 下拉到最底部的状态:释放立即刷新
            case ReleaseToRefresh:
                //下拉进度有时候到不到1,在这里设置一下
                animationView.setProgress(1);
                break;
            case RefreshReleased:
                break;
            // 下拉到最底部后松手的状态:正在刷新
            case Refreshing:
                animationView.setVisibility(INVISIBLE);
                // 刷新动画透明
                if (isTransparent()) {
                    animationView2.setVisibility(GONE);
                } else {
                    AnimUtil.showLocalLottieEffects(animationView2, EffectStep2, true);
                }
                break;
            //结束
            case RefreshFinish:
            case None:
                tvDesc.setVisibility(GONE);
                animationView.setVisibility(INVISIBLE);
                animationView2.setVisibility(GONE);
                animationView2.pauseAnimation();
                break;
        }
    }


    /**
     * 开始动画(开始刷新或者开始加载动画)
     *
     * @param layout        RefreshLayout
     * @param height        HeaderHeight or FooterHeight
     * @param maxDragHeight 最大拖动高度
     */
    @Override
    public void onStartAnimator(@NonNull RefreshLayout layout, int height, int maxDragHeight) {
        // Log.e("下拉刷新", "onStartAnimator  height=" + height + "  maxDragHeight=" + maxDragHeight);
    }

    public void setViewPaddTop(int i) {

        if (view != null) {
            int bottom = (int) SmartUtil.dp2px(10);
            view.setPadding(0, i, 0, bottom);
        }

    }

    public void setTvDesc(String msg) {
        tvDesc.setVisibility(VISIBLE);
        tvDesc.setText(msg);
        animationView.setVisibility(INVISIBLE);
        animationView2.setVisibility(GONE);

    }


    /**
     * 刷新动画是否透明
     */
    public boolean isTransparent() {
        return isTransparent;
    }

    /**
     * 设置刷新动画是否透明
     */
    public void setTransparent(boolean transparent) {
        isTransparent = transparent;
    }

    /**
     * 设置背景颜色
     */
    public void setHeadRefreshViewBackground(int color){
        if (view != null){
            view.setBackgroundColor(color);
        }
    }

}