Skip to content

Commit

Permalink
Exposing some of the function to use in Viewpager2, as currently Circ…
Browse files Browse the repository at this point in the history
…leIndicator supports only for Viewpager

https://meesho.atlassian.net/browse/ANA-201
  • Loading branch information
ashishk09 committed May 3, 2023
1 parent 2daa08c commit 812f4f3
Showing 1 changed file with 60 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 812f4f3

Please sign in to comment.