-
Notifications
You must be signed in to change notification settings - Fork 554
/
PageIndicatorView.java
892 lines (756 loc) · 28.3 KB
/
PageIndicatorView.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
package com.rd;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.text.TextUtilsCompat;
import androidx.viewpager.widget.PagerAdapter;
import androidx.core.view.ViewCompat;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import com.rd.animation.type.*;
import com.rd.draw.controller.DrawController;
import com.rd.draw.data.Indicator;
import com.rd.draw.data.Orientation;
import com.rd.draw.data.PositionSavedState;
import com.rd.draw.data.RtlMode;
import com.rd.utils.CoordinatesUtils;
import com.rd.utils.DensityUtils;
import com.rd.utils.IdUtils;
public class PageIndicatorView extends View implements ViewPager.OnPageChangeListener, IndicatorManager.Listener, ViewPager.OnAdapterChangeListener, View.OnTouchListener {
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
private IndicatorManager manager;
private DataSetObserver setObserver;
private ViewPager viewPager;
private boolean isInteractionEnabled;
public PageIndicatorView(Context context) {
super(context);
init(null);
}
public PageIndicatorView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public PageIndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public PageIndicatorView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
findViewPager(getParent());
}
@Override
protected void onDetachedFromWindow() {
unRegisterSetObserver();
super.onDetachedFromWindow();
}
@Override
public Parcelable onSaveInstanceState() {
Indicator indicator = manager.indicator();
PositionSavedState positionSavedState = new PositionSavedState(super.onSaveInstanceState());
positionSavedState.setSelectedPosition(indicator.getSelectedPosition());
positionSavedState.setSelectingPosition(indicator.getSelectingPosition());
positionSavedState.setLastSelectedPosition(indicator.getLastSelectedPosition());
return positionSavedState;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
if (state instanceof PositionSavedState) {
Indicator indicator = manager.indicator();
PositionSavedState positionSavedState = (PositionSavedState) state;
indicator.setSelectedPosition(positionSavedState.getSelectedPosition());
indicator.setSelectingPosition(positionSavedState.getSelectingPosition());
indicator.setLastSelectedPosition(positionSavedState.getLastSelectedPosition());
super.onRestoreInstanceState(positionSavedState.getSuperState());
} else {
super.onRestoreInstanceState(state);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Pair<Integer, Integer> pair = manager.drawer().measureViewSize(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(pair.first, pair.second);
}
@Override
protected void onDraw(Canvas canvas) {
manager.drawer().draw(canvas);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
manager.drawer().touch(event);
return true;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (!manager.indicator().isFadeOnIdle()) return false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
stopIdleRunnable();
break;
case MotionEvent.ACTION_UP:
startIdleRunnable();
break;
}
return false;
}
@Override
public void onIndicatorUpdated() {
invalidate();
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
onPageScroll(position, positionOffset);
}
@Override
public void onPageSelected(int position) {
onPageSelect(position);
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
manager.indicator().setInteractiveAnimation(isInteractionEnabled);
}
}
@Override
public void onAdapterChanged(@NonNull ViewPager viewPager, @Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) {
if (manager.indicator().isDynamicCount()) {
if (oldAdapter != null && setObserver != null) {
oldAdapter.unregisterDataSetObserver(setObserver);
setObserver = null;
}
registerSetObserver();
}
updateState();
}
/**
* Set static number of circle indicators to be displayed.
*
* @param count total count of indicators.
*/
public void setCount(int count) {
if (count >= 0 && manager.indicator().getCount() != count) {
manager.indicator().setCount(count);
updateVisibility();
requestLayout();
}
}
/**
* Return number of circle indicators
*/
public int getCount() {
return manager.indicator().getCount();
}
/**
* Dynamic count will automatically update number of circle indicators
* if {@link ViewPager} page count updates on run-time. If new count will be bigger than current count,
* selected circle will stay as it is, otherwise it will be set to last one.
* Note: works if {@link ViewPager} set and already have it's adapter. See {@link #setViewPager(ViewPager)}.
*
* @param dynamicCount boolean value to add/remove indicators dynamically.
*/
public void setDynamicCount(boolean dynamicCount) {
manager.indicator().setDynamicCount(dynamicCount);
if (dynamicCount) {
registerSetObserver();
} else {
unRegisterSetObserver();
}
}
/**
* Fade on idle will make {@link PageIndicatorView} {@link View#INVISIBLE} if {@link ViewPager} is not interacted
* in time equal to {@link Indicator#idleDuration}. Take care when setting {@link PageIndicatorView} alpha
* manually if this is true. Alpha is used to manage fading and appearance of {@link PageIndicatorView} and value you provide
* will be overridden when {@link PageIndicatorView} enters or leaves idle state.
*
* @param fadeOnIdle boolean value to hide {@link PageIndicatorView} when {@link ViewPager} is idle
*/
public void setFadeOnIdle(boolean fadeOnIdle) {
manager.indicator().setFadeOnIdle(fadeOnIdle);
if (fadeOnIdle) {
startIdleRunnable();
} else {
stopIdleRunnable();
}
}
/**
* Set radius in dp of each circle indicator. Default value is {@link Indicator#DEFAULT_RADIUS_DP}.
* Note: make sure you set circle Radius, not a Diameter.
*
* @param radiusDp radius of circle in dp.
*/
public void setRadius(int radiusDp) {
if (radiusDp < 0) {
radiusDp = 0;
}
int radiusPx = DensityUtils.dpToPx(radiusDp);
manager.indicator().setRadius(radiusPx);
invalidate();
}
/**
* Set radius in px of each circle indicator. Default value is {@link Indicator#DEFAULT_RADIUS_DP}.
* Note: make sure you set circle Radius, not a Diameter.
*
* @param radiusPx radius of circle in px.
*/
public void setRadius(float radiusPx) {
if (radiusPx < 0) {
radiusPx = 0;
}
manager.indicator().setRadius((int) radiusPx);
invalidate();
}
/**
* Return radius of each circle indicators in px. If custom radius is not set, return
* default value {@link Indicator#DEFAULT_RADIUS_DP}.
*/
public int getRadius() {
return manager.indicator().getRadius();
}
/**
* Set padding in dp between each circle indicator. Default value is {@link Indicator#DEFAULT_PADDING_DP}.
*
* @param paddingDp padding between circles in dp.
*/
public void setPadding(int paddingDp) {
if (paddingDp < 0) {
paddingDp = 0;
}
int paddingPx = DensityUtils.dpToPx(paddingDp);
manager.indicator().setPadding(paddingPx);
invalidate();
}
/**
* Set padding in px between each circle indicator. Default value is {@link Indicator#DEFAULT_PADDING_DP}.
*
* @param paddingPx padding between circles in px.
*/
public void setPadding(float paddingPx) {
if (paddingPx < 0) {
paddingPx = 0;
}
manager.indicator().setPadding((int) paddingPx);
invalidate();
}
/**
* Return padding in px between each circle indicator. If custom padding is not set,
* return default value {@link Indicator#DEFAULT_PADDING_DP}.
*/
public int getPadding() {
return manager.indicator().getPadding();
}
/**
* Set scale factor used in {@link AnimationType#SCALE} animation.
* Defines size of unselected indicator circles in comparing to selected one.
* Minimum and maximum values are {@link ScaleAnimation#MAX_SCALE_FACTOR} and {@link ScaleAnimation#MIN_SCALE_FACTOR}.
* See also {@link ScaleAnimation#DEFAULT_SCALE_FACTOR}.
*
* @param factor float value in range between 0 and 1.
*/
public void setScaleFactor(float factor) {
if (factor > ScaleAnimation.MAX_SCALE_FACTOR) {
factor = ScaleAnimation.MAX_SCALE_FACTOR;
} else if (factor < ScaleAnimation.MIN_SCALE_FACTOR) {
factor = ScaleAnimation.MIN_SCALE_FACTOR;
}
manager.indicator().setScaleFactor(factor);
}
/**
* Returns scale factor values used in {@link AnimationType#SCALE} animation.
* Defines size of unselected indicator circles in comparing to selected one.
* Minimum and maximum values are {@link ScaleAnimation#MAX_SCALE_FACTOR} and {@link ScaleAnimation#MIN_SCALE_FACTOR}.
* See also {@link ScaleAnimation#DEFAULT_SCALE_FACTOR}.
*
* @return float value that indicate scale factor.
*/
public float getScaleFactor() {
return manager.indicator().getScaleFactor();
}
/**
* Set stroke width in px to set while {@link AnimationType#FILL} is selected.
* Default value is {@link FillAnimation#DEFAULT_STROKE_DP}
*
* @param strokePx stroke width in px.
*/
public void setStrokeWidth(float strokePx) {
int radiusPx = manager.indicator().getRadius();
if (strokePx < 0) {
strokePx = 0;
} else if (strokePx > radiusPx) {
strokePx = radiusPx;
}
manager.indicator().setStroke((int) strokePx);
invalidate();
}
/**
* Set stroke width in dp to set while {@link AnimationType#FILL} is selected.
* Default value is {@link FillAnimation#DEFAULT_STROKE_DP}
*
* @param strokeDp stroke width in dp.
*/
public void setStrokeWidth(int strokeDp) {
int strokePx = DensityUtils.dpToPx(strokeDp);
int radiusPx = manager.indicator().getRadius();
if (strokePx < 0) {
strokePx = 0;
} else if (strokePx > radiusPx) {
strokePx = radiusPx;
}
manager.indicator().setStroke(strokePx);
invalidate();
}
/**
* Return stroke width in px if {@link AnimationType#FILL} is selected, 0 otherwise.
*/
public int getStrokeWidth() {
return manager.indicator().getStroke();
}
/**
* Set color of selected state to circle indicator. Default color is {@link ColorAnimation#DEFAULT_SELECTED_COLOR}.
*
* @param color color selected circle.
*/
public void setSelectedColor(int color) {
manager.indicator().setSelectedColor(color);
invalidate();
}
/**
* Return color of selected circle indicator. If custom unselected color
* is not set, return default color {@link ColorAnimation#DEFAULT_SELECTED_COLOR}.
*/
public int getSelectedColor() {
return manager.indicator().getSelectedColor();
}
/**
* Set color of unselected state to each circle indicator. Default color {@link ColorAnimation#DEFAULT_UNSELECTED_COLOR}.
*
* @param color color of each unselected circle.
*/
public void setUnselectedColor(int color) {
manager.indicator().setUnselectedColor(color);
invalidate();
}
/**
* Return color of unselected state of each circle indicator. If custom unselected color
* is not set, return default color {@link ColorAnimation#DEFAULT_UNSELECTED_COLOR}.
*/
public int getUnselectedColor() {
return manager.indicator().getUnselectedColor();
}
/**
* Automatically hide (View.INVISIBLE) PageIndicatorView while indicator count is <= 1.
* Default is true.
*
* @param autoVisibility auto hide indicators.
*/
public void setAutoVisibility(boolean autoVisibility) {
if (!autoVisibility) {
setVisibility(VISIBLE);
}
manager.indicator().setAutoVisibility(autoVisibility);
updateVisibility();
}
/**
* Set orientation for indicator, one of HORIZONTAL or VERTICAL.
* Default is HORIZONTAL.
*
* @param orientation an orientation to display page indicators.
*/
public void setOrientation(@Nullable Orientation orientation) {
if (orientation != null) {
manager.indicator().setOrientation(orientation);
requestLayout();
}
}
/**
* Set animation duration time in millisecond. Default animation duration time is {@link BaseAnimation#DEFAULT_ANIMATION_TIME}.
* (Won't affect on anything unless {@link #setAnimationType(AnimationType type)} is specified
* and {@link #setInteractiveAnimation(boolean isInteractive)} is false).
*
* @param duration animation duration time.
*/
public void setAnimationDuration(long duration) {
manager.indicator().setAnimationDuration(duration);
}
/**
* Sets time in millis after which {@link ViewPager} is considered idle.
* If {@link Indicator#fadeOnIdle} is true, {@link PageIndicatorView} will
* fade away after entering idle state and appear when it is left.
*
* @param duration time in millis after which {@link ViewPager} is considered idle
*/
public void setIdleDuration(long duration) {
manager.indicator().setIdleDuration(duration);
if (manager.indicator().isFadeOnIdle()) {
startIdleRunnable();
} else {
stopIdleRunnable();
}
}
/**
* Return animation duration time in milliseconds. If custom duration is not set,
* return default duration time {@link BaseAnimation#DEFAULT_ANIMATION_TIME}.
*/
public long getAnimationDuration() {
return manager.indicator().getAnimationDuration();
}
/**
* Set animation type to perform while selecting new circle indicator.
* Default animation type is {@link AnimationType#NONE}.
*
* @param type type of animation, one of {@link AnimationType}
*/
public void setAnimationType(@Nullable AnimationType type) {
manager.onValueUpdated(null);
if (type != null) {
manager.indicator().setAnimationType(type);
} else {
manager.indicator().setAnimationType(AnimationType.NONE);
}
invalidate();
}
/**
* Interactive animation will animate indicator smoothly
* from position to position based on user's current swipe progress.
* (Won't affect on anything unless {@link #setViewPager(ViewPager)} is specified).
*
* @param isInteractive value of animation to be interactive or not.
*/
public void setInteractiveAnimation(boolean isInteractive) {
manager.indicator().setInteractiveAnimation(isInteractive);
this.isInteractionEnabled = isInteractive;
}
/**
* Set {@link ViewPager} to add {@link ViewPager.OnPageChangeListener} and automatically
* handle selecting new indicators (and interactive animation effect if it is enabled).
*
* @param pager instance of {@link ViewPager} to work with
*/
@SuppressLint("ClickableViewAccessibility")
public void setViewPager(@Nullable ViewPager pager) {
releaseViewPager();
if (pager == null) {
return;
}
viewPager = pager;
viewPager.addOnPageChangeListener(this);
viewPager.addOnAdapterChangeListener(this);
viewPager.setOnTouchListener(this);
manager.indicator().setViewPagerId(viewPager.getId());
setDynamicCount(manager.indicator().isDynamicCount());
updateState();
}
/**
* Release {@link ViewPager} and stop handling events of {@link ViewPager.OnPageChangeListener}.
*/
public void releaseViewPager() {
if (viewPager != null) {
viewPager.removeOnPageChangeListener(this);
viewPager.removeOnAdapterChangeListener(this);
viewPager = null;
}
}
/**
* Specify to display PageIndicatorView with Right to left layout or not.
* One of {@link RtlMode}: Off (Left to right), On (Right to left)
* or Auto (handle this mode automatically based on users language preferences).
* Default is Off.
*
* @param mode instance of {@link RtlMode}
*/
public void setRtlMode(@Nullable RtlMode mode) {
Indicator indicator = manager.indicator();
if (mode == null) {
indicator.setRtlMode(RtlMode.Off);
} else {
indicator.setRtlMode(mode);
}
if (viewPager == null) {
return;
}
int selectedPosition = indicator.getSelectedPosition();
int position = selectedPosition;
if (isRtl()) {
position = (indicator.getCount() - 1) - selectedPosition;
} else if (viewPager != null) {
position = viewPager.getCurrentItem();
}
indicator.setLastSelectedPosition(position);
indicator.setSelectingPosition(position);
indicator.setSelectedPosition(position);
invalidate();
}
/**
* Return position of currently selected circle indicator.
*/
public int getSelection() {
return manager.indicator().getSelectedPosition();
}
/**
* Set specific circle indicator position to be selected. If position < or > total count,
* accordingly first or last circle indicator will be selected.
*
* @param position position of indicator to select.
*/
public void setSelection(int position) {
Indicator indicator = manager.indicator();
position = adjustPosition(position);
if (position == indicator.getSelectedPosition() || position == indicator.getSelectingPosition()) {
return;
}
indicator.setInteractiveAnimation(false);
indicator.setLastSelectedPosition(indicator.getSelectedPosition());
indicator.setSelectingPosition(position);
indicator.setSelectedPosition(position);
manager.animate().basic();
}
/**
* Set specific circle indicator position to be selected without any kind of animation. If position < or > total count,
* accordingly first or last circle indicator will be selected.
*
* @param position position of indicator to select.
*/
public void setSelected(int position) {
Indicator indicator = manager.indicator();
AnimationType animationType = indicator.getAnimationType();
indicator.setAnimationType(AnimationType.NONE);
setSelection(position);
indicator.setAnimationType(animationType);
}
/**
* Clears selection of all indicators
*/
public void clearSelection() {
//TODO check
Indicator indicator = manager.indicator();
indicator.setInteractiveAnimation(false);
indicator.setLastSelectedPosition(Indicator.COUNT_NONE);
indicator.setSelectingPosition(Indicator.COUNT_NONE);
indicator.setSelectedPosition(Indicator.COUNT_NONE);
manager.animate().basic();
}
/**
* Set progress value in range [0 - 1] to specify state of animation while selecting new circle indicator.
*
* @param selectingPosition selecting position with specific progress value.
* @param progress float value of progress.
*/
public void setProgress(int selectingPosition, float progress) {
Indicator indicator = manager.indicator();
if (!indicator.isInteractiveAnimation()) {
return;
}
int count = indicator.getCount();
if (count <= 0 || selectingPosition < 0) {
selectingPosition = 0;
} else if (selectingPosition > count - 1) {
selectingPosition = count - 1;
}
if (progress < 0) {
progress = 0;
} else if (progress > 1) {
progress = 1;
}
if (progress == 1) {
indicator.setLastSelectedPosition(indicator.getSelectedPosition());
indicator.setSelectedPosition(selectingPosition);
}
indicator.setSelectingPosition(selectingPosition);
manager.animate().interactive(progress);
}
public void setClickListener(@Nullable DrawController.ClickListener listener) {
manager.drawer().setClickListener(listener);
}
private void init(@Nullable AttributeSet attrs) {
setupId();
initIndicatorManager(attrs);
if (manager.indicator().isFadeOnIdle()) {
startIdleRunnable();
}
}
private void setupId() {
if (getId() == NO_ID) {
setId(IdUtils.generateViewId());
}
}
private void initIndicatorManager(@Nullable AttributeSet attrs) {
manager = new IndicatorManager(this);
manager.drawer().initAttributes(getContext(), attrs);
Indicator indicator = manager.indicator();
indicator.setPaddingLeft(getPaddingLeft());
indicator.setPaddingTop(getPaddingTop());
indicator.setPaddingRight(getPaddingRight());
indicator.setPaddingBottom(getPaddingBottom());
isInteractionEnabled = indicator.isInteractiveAnimation();
}
private void registerSetObserver() {
if (setObserver != null || viewPager == null || viewPager.getAdapter() == null) {
return;
}
setObserver = new DataSetObserver() {
@Override
public void onChanged() {
updateState();
}
};
try {
viewPager.getAdapter().registerDataSetObserver(setObserver);
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
private void unRegisterSetObserver() {
if (setObserver == null || viewPager == null || viewPager.getAdapter() == null) {
return;
}
try {
viewPager.getAdapter().unregisterDataSetObserver(setObserver);
setObserver = null;
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
private void updateState() {
if (viewPager == null || viewPager.getAdapter() == null) {
return;
}
int count = viewPager.getAdapter().getCount();
int selectedPos = isRtl() ? (count - 1) - viewPager.getCurrentItem() : viewPager.getCurrentItem();
manager.indicator().setSelectedPosition(selectedPos);
manager.indicator().setSelectingPosition(selectedPos);
manager.indicator().setLastSelectedPosition(selectedPos);
manager.indicator().setCount(count);
manager.animate().end();
updateVisibility();
requestLayout();
}
private void updateVisibility() {
if (!manager.indicator().isAutoVisibility()) {
return;
}
int count = manager.indicator().getCount();
int visibility = getVisibility();
if (visibility != VISIBLE && count > Indicator.MIN_COUNT) {
setVisibility(VISIBLE);
} else if (visibility != INVISIBLE && count <= Indicator.MIN_COUNT) {
setVisibility(View.INVISIBLE);
}
}
private void onPageSelect(int position) {
Indicator indicator = manager.indicator();
boolean canSelectIndicator = isViewMeasured();
int count = indicator.getCount();
if (canSelectIndicator) {
if (isRtl()) {
position = (count - 1) - position;
}
setSelection(position);
}
}
private void onPageScroll(int position, float positionOffset) {
Indicator indicator = manager.indicator();
AnimationType animationType = indicator.getAnimationType();
boolean interactiveAnimation = indicator.isInteractiveAnimation();
boolean canSelectIndicator = isViewMeasured() && interactiveAnimation && animationType != AnimationType.NONE;
if (!canSelectIndicator) {
return;
}
Pair<Integer, Float> progressPair = CoordinatesUtils.getProgress(indicator, position, positionOffset, isRtl());
int selectingPosition = progressPair.first;
float selectingProgress = progressPair.second;
setProgress(selectingPosition, selectingProgress);
}
private boolean isRtl() {
switch (manager.indicator().getRtlMode()) {
case On:
return true;
case Off:
return false;
case Auto:
return TextUtilsCompat.getLayoutDirectionFromLocale(getContext().getResources().getConfiguration().locale) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
return false;
}
private boolean isViewMeasured() {
return getMeasuredHeight() != 0 || getMeasuredWidth() != 0;
}
private void findViewPager(@Nullable ViewParent viewParent) {
boolean isValidParent = viewParent != null &&
viewParent instanceof ViewGroup &&
((ViewGroup) viewParent).getChildCount() > 0;
if (!isValidParent) {
return;
}
int viewPagerId = manager.indicator().getViewPagerId();
ViewPager viewPager = findViewPager((ViewGroup) viewParent, viewPagerId);
if (viewPager != null) {
setViewPager(viewPager);
} else {
findViewPager(viewParent.getParent());
}
}
@Nullable
private ViewPager findViewPager(@NonNull ViewGroup viewGroup, int id) {
if (viewGroup.getChildCount() <= 0) {
return null;
}
View view = viewGroup.findViewById(id);
if (view != null && view instanceof ViewPager) {
return (ViewPager) view;
} else {
return null;
}
}
private int adjustPosition(int position) {
Indicator indicator = manager.indicator();
int count = indicator.getCount();
int lastPosition = count - 1;
if (position < 0) {
position = 0;
} else if (position > lastPosition) {
position = lastPosition;
}
return position;
}
private void displayWithAnimation() {
animate().cancel();
animate().alpha(1.0f).setDuration(Indicator.IDLE_ANIMATION_DURATION);
}
private void hideWithAnimation() {
animate().cancel();
animate().alpha(0f).setDuration(Indicator.IDLE_ANIMATION_DURATION);
}
private void startIdleRunnable() {
HANDLER.removeCallbacks(idleRunnable);
HANDLER.postDelayed(idleRunnable, manager.indicator().getIdleDuration());
}
private void stopIdleRunnable() {
HANDLER.removeCallbacks(idleRunnable);
displayWithAnimation();
}
private Runnable idleRunnable = new Runnable() {
@Override
public void run() {
manager.indicator().setIdle(true);
hideWithAnimation();
}
};
}