Skip to content

Commit 644331a

Browse files
committed
infinite adapter changes.
1 parent 308b9b1 commit 644331a

File tree

4 files changed

+69
-66
lines changed

4 files changed

+69
-66
lines changed

app/src/main/java/com/smarteist/imageslider/MainActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ protected void onCreate(Bundle savedInstanceState) {
3232
adapter = new SliderAdapterExample(this);
3333
sliderView.setIndicatorEnabled(true);
3434
sliderView.setSliderAdapter(adapter);
35-
3635
sliderView.setIndicatorAnimation(IndicatorAnimationType.WORM); //set indicator animation by using SliderLayout.IndicatorAnimations. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!!
3736
sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
3837
sliderView.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_RIGHT);
@@ -45,7 +44,7 @@ protected void onCreate(Bundle savedInstanceState) {
4544
sliderView.setOnIndicatorClickListener(new DrawController.ClickListener() {
4645
@Override
4746
public void onIndicatorClicked(int position) {
48-
sliderView.setInfiniteAdapterEnabled(position % 2 == 0);
47+
sliderView.setCurrentPagePosition(position);
4948
}
5049
});
5150

autoimageslider/src/main/java/com/smarteist/autoimageslider/InfiniteAdapter/InfinitePagerAdapter.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public class InfinitePagerAdapter extends PagerAdapter {
2020
// Warning: it should be an even number.
2121
public static final int INFINITE_SCROLL_LIMIT = 32400;
2222
private static final String TAG = "InfinitePagerAdapter";
23-
2423
private SliderViewAdapter adapter;
25-
private int virtualPosition;
2624

2725
public InfinitePagerAdapter(SliderViewAdapter adapter) {
2826
this.adapter = adapter;
@@ -46,44 +44,51 @@ public int getCount() {
4644
* @return the {@link #getCount()} result of the wrapped adapter
4745
*/
4846
public int getRealCount() {
49-
int count;
5047
try {
51-
count = getRealAdapter().getCount();
48+
return getRealAdapter().getCount();
5249
} catch (Exception e) {
53-
count = 0;
50+
return 0;
5451
}
55-
return count;
52+
}
53+
54+
55+
/**
56+
* @param item real position of item
57+
* @return virtual mid point
58+
*/
59+
public int getMiddlePosition(int item) {
60+
int adapterCount = getRealCount() - 1;
61+
adapterCount = adapterCount == 0 ? 1 : adapterCount;
62+
int midpoint = adapterCount * (InfinitePagerAdapter.INFINITE_SCROLL_LIMIT / 2);
63+
return item + midpoint;
5664
}
5765

5866
@NonNull
5967
@Override
60-
public Object instantiateItem(ViewGroup container, int position) {
68+
public Object instantiateItem(ViewGroup container, int virtualPosition) {
6169
// prevent division by zer
6270
if (getRealCount() < 1) {
6371
return adapter.instantiateItem(container, 0);
6472
}
65-
virtualPosition = position % getRealCount();
66-
67-
//Log.i(TAG, "instantiateItem: real position: " + position);
68-
//Log.i(TAG, "instantiateItem: virtual position: " + virtualPosition);
73+
//Log.i(TAG, "instantiateItem: real virtualPosition: " + virtualPosition);
74+
//Log.i(TAG, "instantiateItem: virtual virtualPosition: " + virtualPosition);
6975

70-
// only expose virtual position to the inner adapter
71-
return adapter.instantiateItem(container, virtualPosition);
76+
// only expose virtual virtualPosition to the inner adapter
77+
return adapter.instantiateItem(container, getRealPosition(virtualPosition));
7278
}
7379

7480
@Override
75-
public void destroyItem(ViewGroup container, int position, Object object) {
81+
public void destroyItem(ViewGroup container, int virtualPosition, Object object) {
7682
// prevent division by zero
7783
if (getRealCount() < 1) {
7884
adapter.destroyItem(container, 0, object);
7985
return;
8086
}
81-
int virtualPosition = position % getRealCount();
8287
//Log.i(TAG, "destroyItem: real position: " + position);
8388
//Log.i(TAG, "destroyItem: virtual position: " + virtualPosition);
8489

8590
// only expose virtual position to the inner adapter
86-
adapter.destroyItem(container, virtualPosition, object);
91+
adapter.destroyItem(container, getRealPosition(virtualPosition), object);
8792
}
8893

8994
@Override
@@ -115,9 +120,8 @@ public Parcelable saveState() {
115120
}
116121

117122
@Override
118-
public CharSequence getPageTitle(int position) {
119-
int virtualPosition = position % getRealCount();
120-
return adapter.getPageTitle(virtualPosition);
123+
public CharSequence getPageTitle(int virtualPosition) {
124+
return adapter.getPageTitle(getRealPosition(virtualPosition));
121125
}
122126

123127
@Override
@@ -145,8 +149,10 @@ public int getItemPosition(Object object) {
145149
return adapter.getItemPosition(object);
146150
}
147151

148-
public int getVirtualPosition() {
149-
return virtualPosition;
152+
public int getRealPosition(int virtualPosition) {
153+
if (getRealCount() > 0) {
154+
return virtualPosition % getRealCount();
155+
}
156+
return 0;
150157
}
151-
152158
}

autoimageslider/src/main/java/com/smarteist/autoimageslider/SliderPager.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import android.view.ViewGroup;
2626
import android.view.ViewParent;
2727
import android.view.accessibility.AccessibilityEvent;
28-
import android.view.animation.DecelerateInterpolator;
2928
import android.view.animation.Interpolator;
3029
import android.widget.EdgeEffect;
3130
import android.widget.Scroller;
@@ -581,7 +580,7 @@ private int getClientWidth() {
581580
*/
582581
public void setCurrentItem(int item) {
583582
mPopulatePending = false;
584-
setCurrentItemInternal(item, !mFirstLayout, false);
583+
setCurrentItem(item, !mFirstLayout);
585584
}
586585

587586
/**
@@ -591,11 +590,17 @@ public void setCurrentItem(int item) {
591590
* @param smoothScroll True to smoothly scroll to the new item, false to transition immediately
592591
*/
593592
public void setCurrentItem(int item, boolean smoothScroll) {
593+
if (mAdapter instanceof InfinitePagerAdapter) {
594+
item = ((InfinitePagerAdapter) mAdapter).getMiddlePosition(item);
595+
}
594596
mPopulatePending = false;
595597
setCurrentItemInternal(item, smoothScroll, false);
596598
}
597599

598600
public int getCurrentItem() {
601+
if (mAdapter instanceof InfinitePagerAdapter && ((InfinitePagerAdapter) mAdapter).getRealCount() > 0) {
602+
return ((InfinitePagerAdapter) mAdapter).getRealPosition(mCurItem);
603+
}
599604
return mCurItem;
600605
}
601606

@@ -1281,12 +1286,7 @@ private void triggerOnPageChangeEvent(int position) {
12811286
for (OnPageChangeListener eachListener : mOnPageChangeListeners) {
12821287
if (eachListener != null) {
12831288
if (mAdapter instanceof InfinitePagerAdapter) {
1284-
InfinitePagerAdapter infiniteAdapter = (InfinitePagerAdapter) mAdapter;
1285-
int realCount = infiniteAdapter.getRealCount();
1286-
if (realCount < 1) {
1287-
return;
1288-
}
1289-
int n = position % realCount;
1289+
int n = ((InfinitePagerAdapter) mAdapter).getRealPosition(position);
12901290
eachListener.onPageSelected(n);
12911291
} else {
12921292
eachListener.onPageSelected(position);

autoimageslider/src/main/java/com/smarteist/autoimageslider/SliderView.java

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import androidx.viewpager.widget.PagerAdapter;
1919

2020
import com.smarteist.autoimageslider.IndicatorView.PageIndicatorView;
21-
import com.smarteist.autoimageslider.IndicatorView.animation.type.IndicatorAnimationType;
2221
import com.smarteist.autoimageslider.IndicatorView.animation.type.BaseAnimation;
2322
import com.smarteist.autoimageslider.IndicatorView.animation.type.ColorAnimation;
23+
import com.smarteist.autoimageslider.IndicatorView.animation.type.IndicatorAnimationType;
2424
import com.smarteist.autoimageslider.IndicatorView.draw.controller.DrawController;
2525
import com.smarteist.autoimageslider.IndicatorView.draw.data.Orientation;
2626
import com.smarteist.autoimageslider.IndicatorView.draw.data.RtlMode;
@@ -409,21 +409,12 @@ public void setSliderAnimationDuration(int duration, Interpolator interpolator)
409409

410410
/**
411411
* This method handles correct position whether slider is on infinite mode or not
412+
*
412413
* @param position changes position of slider
413414
* items manually.
414415
*/
415416
public void setCurrentPagePosition(int position) {
416-
417-
if (getSliderAdapter() != null) {
418-
if (mIsInfiniteAdapter) {
419-
int midpoint = (getAdapterItemsCount() - 1) * (InfinitePagerAdapter.INFINITE_SCROLL_LIMIT / 2);
420-
mSliderPager.setCurrentItem(midpoint + position, true);
421-
} else {
422-
mSliderPager.setCurrentItem(position, true);
423-
}
424-
} else {
425-
throw new NullPointerException("Adapter not set");
426-
}
417+
mSliderPager.setCurrentItem(position, true);
427418
}
428419

429420
/**
@@ -659,20 +650,23 @@ public void slideToNextPosition() {
659650

660651
int currentPosition = mSliderPager.getCurrentItem();
661652
int adapterItemsCount = getAdapterItemsCount();
662-
663-
if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_BACK_AND_FORTH && adapterItemsCount > 1) {
664-
if (currentPosition % (adapterItemsCount - 1) == 0) {
665-
mFlagBackAndForth = !mFlagBackAndForth;
653+
if (adapterItemsCount > 1) {
654+
if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_BACK_AND_FORTH) {
655+
if (currentPosition % (adapterItemsCount - 1) == 0) {
656+
mFlagBackAndForth = !mFlagBackAndForth;
657+
}
658+
if (mFlagBackAndForth) {
659+
mSliderPager.setCurrentItem(++currentPosition, true);
660+
} else {
661+
mSliderPager.setCurrentItem(--currentPosition, true);
662+
}
666663
}
667-
if (mFlagBackAndForth) {
668-
mSliderPager.setCurrentItem(++currentPosition, true);
669-
} else {
664+
if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_LEFT) {
670665
mSliderPager.setCurrentItem(--currentPosition, true);
671666
}
672-
} else if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_LEFT) {
673-
mSliderPager.setCurrentItem(--currentPosition, true);
674-
} else {
675-
mSliderPager.setCurrentItem(++currentPosition, true);
667+
if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_RIGHT) {
668+
mSliderPager.setCurrentItem(++currentPosition, true);
669+
}
676670
}
677671
}
678672

@@ -682,19 +676,23 @@ public void slideToPreviousPosition() {
682676
int currentPosition = mSliderPager.getCurrentItem();
683677
int adapterItemsCount = getAdapterItemsCount();
684678

685-
if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_BACK_AND_FORTH && adapterItemsCount > 1) {
686-
if (currentPosition % (adapterItemsCount - 1) == 0) {
687-
mFlagBackAndForth = !mFlagBackAndForth;
679+
if (adapterItemsCount > 1) {
680+
if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_BACK_AND_FORTH) {
681+
if (currentPosition % (adapterItemsCount - 1) == 0) {
682+
mFlagBackAndForth = !mFlagBackAndForth;
683+
}
684+
if (mFlagBackAndForth) {
685+
mSliderPager.setCurrentItem(--currentPosition, true);
686+
} else {
687+
mSliderPager.setCurrentItem(++currentPosition, true);
688+
}
688689
}
689-
if (mFlagBackAndForth) {
690-
mSliderPager.setCurrentItem(--currentPosition, true);
691-
} else {
690+
if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_LEFT) {
692691
mSliderPager.setCurrentItem(++currentPosition, true);
693692
}
694-
} else if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_LEFT) {
695-
mSliderPager.setCurrentItem(++currentPosition, true);
696-
} else {
697-
mSliderPager.setCurrentItem(--currentPosition, true);
693+
if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_RIGHT) {
694+
mSliderPager.setCurrentItem(--currentPosition, true);
695+
}
698696
}
699697
}
700698

@@ -703,7 +701,7 @@ public void slideToPreviousPosition() {
703701
public void dataSetChanged() {
704702
if (mIsInfiniteAdapter) {
705703
mInfinitePagerAdapter.notifyDataSetChanged();
706-
mSliderPager.setCurrentItem((getAdapterItemsCount() - 1) * (InfinitePagerAdapter.INFINITE_SCROLL_LIMIT / 2), false);
704+
mSliderPager.setCurrentItem(0, false);
707705
}
708706
}
709707

0 commit comments

Comments
 (0)