CustomRefreshHeader.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.cooleshow.base.widgets;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.widget.FrameLayout;
  7. import com.airbnb.lottie.LottieAnimationView;
  8. import com.cooleshow.base.R;
  9. import com.cooleshow.base.utils.LogUtils;
  10. import com.scwang.smart.refresh.layout.api.RefreshHeader;
  11. import com.scwang.smart.refresh.layout.api.RefreshKernel;
  12. import com.scwang.smart.refresh.layout.api.RefreshLayout;
  13. import com.scwang.smart.refresh.layout.constant.RefreshState;
  14. import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
  15. import androidx.annotation.NonNull;
  16. import androidx.annotation.Nullable;
  17. /**
  18. * Author by pq, Date on 2022/7/29.
  19. */
  20. public class CustomRefreshHeader extends FrameLayout implements RefreshHeader {
  21. private LottieAnimationView mViewAnim;
  22. public CustomRefreshHeader(@NonNull Context context) {
  23. this(context, null);
  24. }
  25. public CustomRefreshHeader(@NonNull Context context, @Nullable AttributeSet attrs) {
  26. this(context, attrs, -1);
  27. }
  28. public CustomRefreshHeader(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  29. super(context, attrs, defStyleAttr);
  30. init();
  31. }
  32. private void init() {
  33. LayoutInflater.from(getContext()).inflate(R.layout.view_custom_refresh_header_layout, this);
  34. mViewAnim = findViewById(R.id.view_anim);
  35. mViewAnim.setAnimation("lottie/refresh_anim.json");
  36. mViewAnim.loop(true);
  37. mViewAnim.setVisibility(View.GONE);
  38. }
  39. @NonNull
  40. @Override
  41. public View getView() {
  42. return this;
  43. }
  44. @NonNull
  45. @Override
  46. public SpinnerStyle getSpinnerStyle() {
  47. return SpinnerStyle.Translate;
  48. }
  49. @Override
  50. public void setPrimaryColors(int... colors) {
  51. }
  52. @Override
  53. public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
  54. }
  55. @Override
  56. public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
  57. LogUtils.i("pq", "onMoving isDragging:" + isDragging + "\npercent:" + percent + "\noffset:" + offset
  58. + "\nheight:" + height + "\nmaxDragHeight:" + maxDragHeight);
  59. }
  60. @Override
  61. public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
  62. LogUtils.i("pq", "onReleased height:" + height + "\nmaxDragHeight:" + maxDragHeight);
  63. }
  64. @Override
  65. public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
  66. }
  67. @Override
  68. public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
  69. if (mViewAnim != null) {
  70. mViewAnim.cancelAnimation();
  71. mViewAnim.clearAnimation();
  72. }
  73. LogUtils.i("pq", "onFinish");
  74. return 500;//延迟500毫秒之后再弹回;
  75. }
  76. @Override
  77. public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
  78. }
  79. @Override
  80. public boolean isSupportHorizontalDrag() {
  81. return false;
  82. }
  83. @Override
  84. public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
  85. switch (newState) {
  86. case None:
  87. LogUtils.i("pq", "None");
  88. mViewAnim.setFrame(0);
  89. mViewAnim.setProgress(0);
  90. break;
  91. case PullDownToRefresh:
  92. LogUtils.i("pq", "PullDownToRefresh");
  93. playAnim();
  94. mViewAnim.setVisibility(View.VISIBLE);
  95. break;
  96. case PullDownCanceled:
  97. if (mViewAnim != null) {
  98. clearAnim();
  99. }
  100. LogUtils.i("pq", "PullDownCanceled");
  101. break;
  102. case ReleaseToRefresh:
  103. LogUtils.i("pq", "ReleaseToRefresh");
  104. mViewAnim.setVisibility(View.VISIBLE);
  105. break;
  106. case Refreshing:
  107. LogUtils.i("pq", "Refreshing");
  108. break;
  109. case RefreshReleased:
  110. LogUtils.i("pq", "RefreshReleased");
  111. break;
  112. case RefreshFinish:
  113. LogUtils.i("pq", "RefreshFinish");
  114. break;
  115. }
  116. }
  117. private void playAnim() {
  118. if (mViewAnim != null) {
  119. mViewAnim.playAnimation();
  120. }
  121. }
  122. private void clearAnim() {
  123. if (mViewAnim != null) {
  124. mViewAnim.cancelAnimation();
  125. mViewAnim.clearAnimation();
  126. }
  127. }
  128. }