diff --git a/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java b/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java index a030eff..acbcc06 100644 --- a/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java +++ b/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java @@ -5,6 +5,8 @@ import android.database.DataSetObserver; import android.os.Build; import android.util.AttributeSet; +import android.view.View; + import androidx.annotation.Nullable; import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; @@ -44,15 +46,20 @@ public void setViewPager(@Nullable ViewPager viewPager) { } } + protected int getCount(ViewPager viewPager) { + return viewPager.getAdapter() != null ? viewPager.getAdapter().getCount() : 0; + } + + protected int initialPosition(ViewPager viewPager) { + return viewPager.getCurrentItem(); + } + private void createIndicators() { - PagerAdapter adapter = mViewpager.getAdapter(); - int count; - if (adapter == null) { - count = 0; - } else { - count = adapter.getCount(); - } - createIndicators(count, mViewpager.getCurrentItem()); + createIndicators(getCount(mViewpager), initialPosition(mViewpager)); + } + + protected int getCurrentPosition(ViewPager viewPager, int position) { + return position; } private final ViewPager.OnPageChangeListener mInternalPageChangeListener = @@ -63,12 +70,11 @@ private void createIndicators() { } @Override public void onPageSelected(int position) { - if (mViewpager.getAdapter() == null || mViewpager.getAdapter().getCount() <= 0) { return; } - animatePageSelected(position); + animatePageSelected(getCurrentPosition(mViewpager, position)); } @Override public void onPageScrollStateChanged(int state) {