-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathMaterialRefreshLayout.java
executable file
·562 lines (480 loc) · 21.1 KB
/
MaterialRefreshLayout.java
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package com.cjj;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.FrameLayout;
public class MaterialRefreshLayout extends FrameLayout {
public static final String Tag = MaterialRefreshLayout.class.getSimpleName();
private final static int DEFAULT_WAVE_HEIGHT = 140;
private final static int HIGHER_WAVE_HEIGHT = 180;
private final static int DEFAULT_HEAD_HEIGHT = 70;
private final static int hIGHER_HEAD_HEIGHT = 100;
private final static int DEFAULT_PROGRESS_SIZE = 50;
private final static int BIG_PROGRESS_SIZE = 60;
private final static int PROGRESS_STOKE_WIDTH = 3;
private MaterialHeaderView mMaterialHeaderView;
private MaterialFooterView mMaterialFooterView;
private SunLayout mSunLayout;
private boolean isOverlay;
private int waveType;
private int waveColor;
protected float mWaveHeight;
protected float mHeadHeight;
private View mChildView;
protected boolean isRefreshing;
private float mTouchY;
private float mCurrentY;
private DecelerateInterpolator decelerateInterpolator;
private float headHeight;
private float waveHeight;
private int[] colorSchemeColors;
private int colorsId;
private int progressTextColor;
private int progressValue, progressMax;
private boolean showArrow = true;
private int textType;
private MaterialRefreshListener refreshListener;
private boolean showProgressBg;
private int progressBg;
private boolean isShowWave;
private int progressSizeType;
private int progressSize = 0;
private boolean isLoadMoreing;
private boolean isLoadMore;
private boolean isSunStyle = false;
public MaterialRefreshLayout(Context context) {
this(context, null, 0);
}
public MaterialRefreshLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MaterialRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defstyleAttr) {
if (isInEditMode()) {
return;
}
if (getChildCount() > 1) {
throw new RuntimeException("can only have one child widget");
}
decelerateInterpolator = new DecelerateInterpolator(10);
TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.MaterialRefreshLayout, defstyleAttr, 0);
isOverlay = t.getBoolean(R.styleable.MaterialRefreshLayout_overlay, false);
/**attrs for materialWaveView*/
waveType = t.getInt(R.styleable.MaterialRefreshLayout_wave_height_type, 0);
if (waveType == 0) {
headHeight = DEFAULT_HEAD_HEIGHT;
waveHeight = DEFAULT_WAVE_HEIGHT;
MaterialWaveView.DefaulHeadHeight = DEFAULT_HEAD_HEIGHT;
MaterialWaveView.DefaulWaveHeight = DEFAULT_WAVE_HEIGHT;
} else {
headHeight = hIGHER_HEAD_HEIGHT;
waveHeight = HIGHER_WAVE_HEIGHT;
MaterialWaveView.DefaulHeadHeight = hIGHER_HEAD_HEIGHT;
MaterialWaveView.DefaulWaveHeight = HIGHER_WAVE_HEIGHT;
}
waveColor = t.getColor(R.styleable.MaterialRefreshLayout_wave_color, Color.WHITE);
isShowWave = t.getBoolean(R.styleable.MaterialRefreshLayout_wave_show, true);
/**attrs for circleprogressbar*/
colorsId = t.getResourceId(R.styleable.MaterialRefreshLayout_progress_colors, R.array.material_colors);
colorSchemeColors = context.getResources().getIntArray(colorsId);
showArrow = t.getBoolean(R.styleable.MaterialRefreshLayout_progress_show_arrow, true);
textType = t.getInt(R.styleable.MaterialRefreshLayout_progress_text_visibility, 1);
progressTextColor = t.getColor(R.styleable.MaterialRefreshLayout_progress_text_color, Color.BLACK);
progressValue = t.getInteger(R.styleable.MaterialRefreshLayout_progress_value, 0);
progressMax = t.getInteger(R.styleable.MaterialRefreshLayout_progress_max_value, 100);
showProgressBg = t.getBoolean(R.styleable.MaterialRefreshLayout_progress_show_circle_backgroud, true);
progressBg = t.getColor(R.styleable.MaterialRefreshLayout_progress_backgroud_color, CircleProgressBar.DEFAULT_CIRCLE_BG_LIGHT);
progressSizeType = t.getInt(R.styleable.MaterialRefreshLayout_progress_size_type, 0);
if (progressSizeType == 0) {
progressSize = DEFAULT_PROGRESS_SIZE;
} else {
progressSize = BIG_PROGRESS_SIZE;
}
isLoadMore = t.getBoolean(R.styleable.MaterialRefreshLayout_isLoadMore, false);
t.recycle();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.i(Tag, "onAttachedToWindow");
Context context = getContext();
mChildView = getChildAt(0);
if (mChildView == null) {
return;
}
setWaveHeight(Util.dip2px(context, waveHeight));
setHeaderHeight(Util.dip2px(context, headHeight));
if (isSunStyle) {
mSunLayout = new SunLayout(context);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Util.dip2px(context, hIGHER_HEAD_HEIGHT));
layoutParams.gravity = Gravity.TOP;
mSunLayout.setVisibility(View.GONE);
setHeaderView(mSunLayout);
} else {
mMaterialHeaderView = new MaterialHeaderView(context);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Util.dip2px(context, hIGHER_HEAD_HEIGHT));
layoutParams.gravity = Gravity.TOP;
mMaterialHeaderView.setLayoutParams(layoutParams);
mMaterialHeaderView.setWaveColor(isShowWave ? waveColor : Color.TRANSPARENT);
mMaterialHeaderView.showProgressArrow(showArrow);
mMaterialHeaderView.setProgressSize(progressSize);
mMaterialHeaderView.setProgressColors(colorSchemeColors);
mMaterialHeaderView.setProgressStokeWidth(PROGRESS_STOKE_WIDTH);
mMaterialHeaderView.setTextType(textType);
mMaterialHeaderView.setProgressTextColor(progressTextColor);
mMaterialHeaderView.setProgressValue(progressValue);
mMaterialHeaderView.setProgressValueMax(progressMax);
mMaterialHeaderView.setIsProgressBg(showProgressBg);
mMaterialHeaderView.setProgressBg(progressBg);
mMaterialHeaderView.setVisibility(View.GONE);
setHeaderView(mMaterialHeaderView);
}
mMaterialFooterView = new MaterialFooterView(context);
LayoutParams layoutParams2 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Util.dip2px(context, hIGHER_HEAD_HEIGHT));
layoutParams2.gravity = Gravity.BOTTOM;
mMaterialFooterView.setLayoutParams(layoutParams2);
mMaterialFooterView.showProgressArrow(showArrow);
mMaterialFooterView.setProgressSize(progressSize);
mMaterialFooterView.setProgressColors(colorSchemeColors);
mMaterialFooterView.setProgressStokeWidth(PROGRESS_STOKE_WIDTH);
mMaterialFooterView.setTextType(textType);
mMaterialFooterView.setProgressValue(progressValue);
mMaterialFooterView.setProgressValueMax(progressMax);
mMaterialFooterView.setIsProgressBg(showProgressBg);
mMaterialFooterView.setProgressBg(progressBg);
mMaterialFooterView.setVisibility(View.GONE);
setFooderView(mMaterialFooterView);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isRefreshing) return true;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mTouchY = ev.getY();
mCurrentY = mTouchY;
break;
case MotionEvent.ACTION_MOVE:
float currentY = ev.getY();
float dy = currentY - mTouchY;
if (dy > 0 && !canChildScrollUp()) {
if (mMaterialHeaderView != null) {
mMaterialHeaderView.setVisibility(View.VISIBLE);
mMaterialHeaderView.onBegin(this);
} else if (mSunLayout != null) {
mSunLayout.setVisibility(View.VISIBLE);
mSunLayout.onBegin(this);
}
return true;
} else if (dy < 0 && !canChildScrollDown() && isLoadMore) {
if (mMaterialFooterView != null && !isLoadMoreing) {
soveLoadMoreLogic();
}
return super.onInterceptTouchEvent(ev);
}
break;
}
return super.onInterceptTouchEvent(ev);
}
private void soveLoadMoreLogic() {
isLoadMoreing = true;
mMaterialFooterView.setVisibility(View.VISIBLE);
mMaterialFooterView.onBegin(this);
mMaterialFooterView.onRefreshing(this);
if (refreshListener != null) {
refreshListener.onRefreshLoadMore(MaterialRefreshLayout.this);
}
}
@Override
public boolean onTouchEvent(MotionEvent e) {
if (isRefreshing) {
return super.onTouchEvent(e);
}
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
mCurrentY = e.getY();
float dy = mCurrentY - mTouchY;
dy = Math.min(mWaveHeight * 2, dy);
dy = Math.max(0, dy);
if (mChildView != null) {
float offsetY = decelerateInterpolator.getInterpolation(dy / mWaveHeight / 2) * dy / 2;
float fraction = offsetY / mHeadHeight;
if (mMaterialHeaderView != null) {
mMaterialHeaderView.getLayoutParams().height = (int) offsetY;
mMaterialHeaderView.requestLayout();
mMaterialHeaderView.onPull(this, fraction);
} else if (mSunLayout != null) {
mSunLayout.getLayoutParams().height = (int) offsetY;
mSunLayout.requestLayout();
mSunLayout.onPull(this, fraction);
}
if (!isOverlay)
ViewCompat.setTranslationY(mChildView, offsetY);
}
return true;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (mChildView != null) {
if (mMaterialHeaderView != null) {
if (isOverlay) {
if (mMaterialHeaderView.getLayoutParams().height > mHeadHeight) {
updateListener();
mMaterialHeaderView.getLayoutParams().height = (int) mHeadHeight;
mMaterialHeaderView.requestLayout();
} else {
mMaterialHeaderView.getLayoutParams().height = 0;
mMaterialHeaderView.requestLayout();
}
} else {
if (ViewCompat.getTranslationY(mChildView) >= mHeadHeight) {
createAnimatorTranslationY(mChildView, mHeadHeight, mMaterialHeaderView);
updateListener();
} else {
createAnimatorTranslationY(mChildView, 0, mMaterialHeaderView);
}
}
} else if (mSunLayout != null) {
if (isOverlay) {
if (mSunLayout.getLayoutParams().height > mHeadHeight) {
updateListener();
mSunLayout.getLayoutParams().height = (int) mHeadHeight;
mSunLayout.requestLayout();
} else {
mSunLayout.getLayoutParams().height = 0;
mSunLayout.requestLayout();
}
} else {
if (ViewCompat.getTranslationY(mChildView) >= mHeadHeight) {
createAnimatorTranslationY(mChildView, mHeadHeight, mSunLayout);
updateListener();
} else {
createAnimatorTranslationY(mChildView, 0, mSunLayout);
}
}
}
}
return true;
}
return super.
onTouchEvent(e);
}
public void setSunStyle(boolean isSunStyle) {
this.isSunStyle = isSunStyle;
}
public void autoRefresh() {
this.postDelayed(new Runnable() {
@Override
public void run() {
if (!isRefreshing) {
if (mMaterialHeaderView != null) {
mMaterialHeaderView.setVisibility(View.VISIBLE);
if (isOverlay) {
mMaterialHeaderView.getLayoutParams().height = (int) mHeadHeight;
mMaterialHeaderView.requestLayout();
} else {
createAnimatorTranslationY(mChildView, mHeadHeight, mMaterialHeaderView);
}
} else if (mSunLayout != null) {
mSunLayout.setVisibility(View.VISIBLE);
if (isOverlay) {
mSunLayout.getLayoutParams().height = (int) mHeadHeight;
mSunLayout.requestLayout();
} else {
createAnimatorTranslationY(mChildView, mHeadHeight, mSunLayout);
}
}
updateListener();
}
}
}, 50);
}
public void autoRefreshLoadMore() {
this.post(new Runnable() {
@Override
public void run() {
if (isLoadMore) {
soveLoadMoreLogic();
} else {
throw new RuntimeException("you must setLoadMore ture");
}
}
});
}
public void updateListener() {
isRefreshing = true;
if (mMaterialHeaderView != null) {
mMaterialHeaderView.onRefreshing(MaterialRefreshLayout.this);
} else if (mSunLayout != null) {
mSunLayout.onRefreshing(MaterialRefreshLayout.this);
}
if (refreshListener != null) {
refreshListener.onRefresh(MaterialRefreshLayout.this);
}
}
public void setLoadMore(boolean isLoadMore) {
this.isLoadMore = isLoadMore;
}
public void setProgressColors(int[] colors) {
this.colorSchemeColors = colors;
}
public void setShowArrow(boolean showArrow) {
this.showArrow = showArrow;
}
public void setShowProgressBg(boolean showProgressBg) {
this.showProgressBg = showProgressBg;
}
public void setWaveColor(int waveColor) {
this.waveColor = waveColor;
}
public void setWaveShow(boolean isShowWave) {
this.isShowWave = isShowWave;
}
public void setIsOverLay(boolean isOverLay) {
this.isOverlay = isOverLay;
}
// public void setProgressValue(int progressValue) {
// this.progressValue = progressValue;
// mMaterialHeaderView.setProgressValue(progressValue);
// }
public void createAnimatorTranslationY(final View v, final float h, final FrameLayout fl) {
//use ObjectAnimator instead of ViewPropertyAnimatorCompat
//because Prior to API 19, ViewPropertyAnimatorUpdateListener will do nothing
ObjectAnimator animator = ObjectAnimator.ofFloat(v, "translationY", h);
animator.setDuration(250);
animator.setInterpolator(new DecelerateInterpolator());
animator.start();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float height = ViewCompat.getTranslationY(v);
fl.getLayoutParams().height = (int) height;
fl.requestLayout();
}
});
}
/**
* @return Whether it is possible for the child view of this layout to
* scroll up. Override this if the child view is a custom view.
*/
public boolean canChildScrollUp() {
if (mChildView == null) {
return false;
}
if (Build.VERSION.SDK_INT < 14) {
if (mChildView instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mChildView;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return ViewCompat.canScrollVertically(mChildView, -1) || mChildView.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mChildView, -1);
}
}
public boolean canChildScrollDown() {
if (mChildView == null) {
return false;
}
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mChildView instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mChildView;
if (absListView.getChildCount() > 0) {
int lastChildBottom = absListView.getChildAt(absListView.getChildCount() - 1).getBottom();
return absListView.getLastVisiblePosition() == absListView.getAdapter().getCount() - 1 && lastChildBottom <= absListView.getMeasuredHeight();
} else {
return false;
}
} else {
return ViewCompat.canScrollVertically(mChildView, 1) || mChildView.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mChildView, 1);
}
}
public void setWaveHigher() {
headHeight = hIGHER_HEAD_HEIGHT;
waveHeight = HIGHER_WAVE_HEIGHT;
MaterialWaveView.DefaulHeadHeight = hIGHER_HEAD_HEIGHT;
MaterialWaveView.DefaulWaveHeight = HIGHER_WAVE_HEIGHT;
}
public void finishRefreshing() {
if (mChildView != null) {
ViewPropertyAnimatorCompat viewPropertyAnimatorCompat = ViewCompat.animate(mChildView);
viewPropertyAnimatorCompat.setDuration(200);
viewPropertyAnimatorCompat.y(ViewCompat.getTranslationY(mChildView));
viewPropertyAnimatorCompat.translationY(0);
viewPropertyAnimatorCompat.setInterpolator(new DecelerateInterpolator());
viewPropertyAnimatorCompat.start();
if (mMaterialHeaderView != null) {
mMaterialHeaderView.onComlete(MaterialRefreshLayout.this);
} else if (mSunLayout != null) {
mSunLayout.onComlete(MaterialRefreshLayout.this);
}
if (refreshListener != null) {
refreshListener.onfinish();
}
}
isRefreshing = false;
progressValue = 0;
}
public void finishRefresh() {
this.post(new Runnable() {
@Override
public void run() {
finishRefreshing();
}
});
}
public void finishRefreshLoadMore() {
this.post(new Runnable() {
@Override
public void run() {
if (mMaterialFooterView != null && isLoadMoreing) {
isLoadMoreing = false;
mMaterialFooterView.onComlete(MaterialRefreshLayout.this);
}
}
});
}
private void setHeaderView(final View headerView) {
addView(headerView);
}
public void setHeader(final View headerView) {
setHeaderView(headerView);
}
public void setFooderView(final View fooderView) {
this.addView(fooderView);
}
public void setWaveHeight(float waveHeight) {
this.mWaveHeight = waveHeight;
}
public void setHeaderHeight(float headHeight) {
this.mHeadHeight = headHeight;
}
public void setMaterialRefreshListener(MaterialRefreshListener refreshListener) {
this.refreshListener = refreshListener;
}
public boolean getIsRefreshing() {
return isRefreshing;
}
public boolean getIsLoadMoreing() {
return isLoadMoreing;
}
}