CommonRefreshHeader.java
6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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);
}
}
}