Skip to content

Commit

Permalink
Merge pull request #3 from Meesho/changes_for_viewpager2
Browse files Browse the repository at this point in the history
ANA-201 : Exposing some of the function to use for Viewpager2
  • Loading branch information
ashishk09 authored May 9, 2023
2 parents 2daa08c + 812f4f3 commit 5665698
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 5665698

Please sign in to comment.