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) {