From 812f4f38bb50f720f5e6207719fcdf2e37e84f2d Mon Sep 17 00:00:00 2001 From: ashishk09 Date: Wed, 3 May 2023 16:01:32 +0530 Subject: [PATCH] Exposing some of the function to use in Viewpager2, as currently CircleIndicator supports only for Viewpager https://meesho.atlassian.net/browse/ANA-201 --- .../circleindicator/CircleIndicator.java | 92 ++++++++++++------- 1 file changed, 60 insertions(+), 32 deletions(-) diff --git a/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java b/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java index 965a58a..ec045f9 100644 --- a/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java +++ b/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java @@ -183,31 +183,7 @@ public void onPageSelected(int position) { if (mViewpager.getAdapter() == null || mViewpager.getAdapter().getCount() <= 0) { return; } - - if (mAnimatorIn.isRunning()) { - mAnimatorIn.end(); - mAnimatorIn.cancel(); - } - - if (mAnimatorOut.isRunning()) { - mAnimatorOut.end(); - mAnimatorOut.cancel(); - } - - View currentIndicator; - if (mLastPosition >= 0 && (currentIndicator = getChildAt(mLastPosition)) != null) { - currentIndicator.setBackgroundResource(mIndicatorUnselectedBackgroundResId); - mAnimatorIn.setTarget(currentIndicator); - mAnimatorIn.start(); - } - - View selectedIndicator = getChildAt(position); - if (selectedIndicator != null) { - selectedIndicator.setBackgroundResource(mIndicatorBackgroundResId); - mAnimatorOut.setTarget(selectedIndicator); - mAnimatorOut.start(); - } - mLastPosition = position; + animatePageSelected(position); } @Override @@ -271,14 +247,35 @@ private void createIndicators() { return; } int currentItem = mViewpager.getCurrentItem(); - int orientation = getOrientation(); + createIndicators(count, currentItem); - for (int i = 0; i < count; i++) { - if (currentItem == i) { - addIndicator(orientation, mIndicatorBackgroundResId, mImmediateAnimatorOut); - } else { - addIndicator(orientation, mIndicatorUnselectedBackgroundResId, - mImmediateAnimatorIn); + } + + public void createIndicators(int count, int currentPosition) { + if (mAnimatorIn.isRunning()) { + mAnimatorIn.end(); + mAnimatorIn.cancel(); + } + + if (mAnimatorOut.isRunning()) { + mAnimatorOut.end(); + mAnimatorOut.cancel(); + } + + // Diff View + int childViewCount = getChildCount(); + if (count < childViewCount) { + removeViews(count, childViewCount - count); + } else if (count > childViewCount) { + int addCount = count - childViewCount; + int orientation = getOrientation(); + for (int i = 0; i < addCount; i++) { + if (currentPosition == i) { + addIndicator(orientation, mIndicatorBackgroundResId, mImmediateAnimatorOut); + } else { + addIndicator(orientation, mIndicatorUnselectedBackgroundResId, + mImmediateAnimatorIn); + } } } } @@ -309,6 +306,37 @@ private void addIndicator(int orientation, @DrawableRes int backgroundDrawableId animator.start(); } + public void animatePageSelected(int position) { + if (mLastPosition == position) { + return; + } + + if (mAnimatorIn.isRunning()) { + mAnimatorIn.end(); + mAnimatorIn.cancel(); + } + + if (mAnimatorOut.isRunning()) { + mAnimatorOut.end(); + mAnimatorOut.cancel(); + } + + View currentIndicator; + if (mLastPosition >= 0 && (currentIndicator = getChildAt(mLastPosition)) != null) { + currentIndicator.setBackgroundResource(mIndicatorUnselectedBackgroundResId); + mAnimatorIn.setTarget(currentIndicator); + mAnimatorIn.start(); + } + + View selectedIndicator = getChildAt(position); + if (selectedIndicator != null) { + selectedIndicator.setBackgroundResource(mIndicatorBackgroundResId); + mAnimatorOut.setTarget(selectedIndicator); + mAnimatorOut.start(); + } + mLastPosition = position; + } + private static class ReverseInterpolator implements Interpolator { @Override public float getInterpolation(float value) {