123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package com.cooleshow.base.widgets;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.FrameLayout;
- import com.airbnb.lottie.LottieAnimationView;
- import com.cooleshow.base.R;
- import com.cooleshow.base.utils.LogUtils;
- import com.scwang.smart.refresh.layout.api.RefreshHeader;
- import com.scwang.smart.refresh.layout.api.RefreshKernel;
- 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 androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- /**
- * Author by pq, Date on 2022/7/29.
- */
- public class CustomRefreshHeader extends FrameLayout implements RefreshHeader {
- private LottieAnimationView mViewAnim;
- public CustomRefreshHeader(@NonNull Context context) {
- this(context, null);
- }
- public CustomRefreshHeader(@NonNull Context context, @Nullable AttributeSet attrs) {
- this(context, attrs, -1);
- }
- public CustomRefreshHeader(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init();
- }
- private void init() {
- LayoutInflater.from(getContext()).inflate(R.layout.view_custom_refresh_header_layout, this);
- mViewAnim = findViewById(R.id.view_anim);
- mViewAnim.setAnimation("lottie/refresh_anim.json");
- mViewAnim.loop(true);
- mViewAnim.setVisibility(View.GONE);
- }
- @NonNull
- @Override
- public View getView() {
- return this;
- }
- @NonNull
- @Override
- public SpinnerStyle getSpinnerStyle() {
- return SpinnerStyle.Translate;
- }
- @Override
- public void setPrimaryColors(int... colors) {
- }
- @Override
- public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
- }
- @Override
- public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
- LogUtils.i("pq", "onMoving isDragging:" + isDragging + "\npercent:" + percent + "\noffset:" + offset
- + "\nheight:" + height + "\nmaxDragHeight:" + maxDragHeight);
- }
- @Override
- public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
- LogUtils.i("pq", "onReleased height:" + height + "\nmaxDragHeight:" + maxDragHeight);
- }
- @Override
- public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
- }
- @Override
- public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
- if (mViewAnim != null) {
- mViewAnim.cancelAnimation();
- mViewAnim.clearAnimation();
- }
- LogUtils.i("pq", "onFinish");
- return 500;//延迟500毫秒之后再弹回;
- }
- @Override
- public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
- }
- @Override
- public boolean isSupportHorizontalDrag() {
- return false;
- }
- @Override
- public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
- switch (newState) {
- case None:
- LogUtils.i("pq", "None");
- mViewAnim.setFrame(0);
- mViewAnim.setProgress(0);
- break;
- case PullDownToRefresh:
- LogUtils.i("pq", "PullDownToRefresh");
- playAnim();
- mViewAnim.setVisibility(View.VISIBLE);
- break;
- case PullDownCanceled:
- if (mViewAnim != null) {
- clearAnim();
- }
- LogUtils.i("pq", "PullDownCanceled");
- break;
- case ReleaseToRefresh:
- LogUtils.i("pq", "ReleaseToRefresh");
- mViewAnim.setVisibility(View.VISIBLE);
- break;
- case Refreshing:
- LogUtils.i("pq", "Refreshing");
- break;
- case RefreshReleased:
- LogUtils.i("pq", "RefreshReleased");
- break;
- case RefreshFinish:
- LogUtils.i("pq", "RefreshFinish");
- break;
- }
- }
- private void playAnim() {
- if (mViewAnim != null) {
- mViewAnim.playAnimation();
- }
- }
- private void clearAnim() {
- if (mViewAnim != null) {
- mViewAnim.cancelAnimation();
- mViewAnim.clearAnimation();
- }
- }
- }
|