2222
2323import java .util .List ;
2424
25+ import static com .crazysunj .cardslideview .CardViewPager .CACHE_COUNT ;
26+
2527/**
2628 * description
2729 * <p>viewPager适配器
@@ -31,6 +33,8 @@ class CardPagerAdapter extends FragmentStatePagerAdapter {
3133
3234 private static final int MAX_VALUE = Integer .MAX_VALUE ;
3335
36+ static final int DIFF_COUNT = CACHE_COUNT / 2 ;
37+
3438 private List <CardItem > mCardItems ;
3539 private boolean mIsLoop ;
3640
@@ -41,7 +45,9 @@ class CardPagerAdapter extends FragmentStatePagerAdapter {
4145 }
4246
4347 void setCardMode (@ CardViewPager .TransformerMode int mode ) {
44- if (mCardItems == null || mCardItems .isEmpty ()) return ;
48+ if (mCardItems == null || mCardItems .isEmpty ()) {
49+ return ;
50+ }
4551 for (CardItem cardItem : mCardItems ) {
4652 cardItem .currentMode = mode ;
4753 }
@@ -54,12 +60,17 @@ public Fragment getItem(int position) {
5460
5561 @ Override
5662 public int getCount () {
57- return mIsLoop ? MAX_VALUE : getRealCount ();
63+ final int realCount = getRealCount ();
64+ if (realCount == 0 ) {
65+ return 0 ;
66+ }
67+ return mIsLoop ? MAX_VALUE : realCount ;
5868 }
5969
6070 @ Override
6171 public Object instantiateItem (ViewGroup container , int position ) {
62- return super .instantiateItem (container , mIsLoop ? position % getRealCount () : position );
72+ final int j = position % getRealCount ();
73+ return super .instantiateItem (container , mIsLoop ? j : position );
6374 }
6475
6576 @ Override
@@ -69,11 +80,7 @@ public void destroyItem(ViewGroup container, int position, Object object) {
6980 int pos = viewPager .getCurrentItem ();
7081 int i = pos % getRealCount ();
7182 int j = position % getRealCount ();
72- if (viewPager .isNotify ) {
73- super .destroyItem (container , j , object );
74- return ;
75- }
76- if (viewPager .isCardMode () && j >= i - 2 && j <= i + 2 ) {
83+ if (Math .abs (i - j ) != DIFF_COUNT && !viewPager .isNotify ) {
7784 return ;
7885 }
7986 super .destroyItem (container , j , object );
@@ -85,14 +92,17 @@ public void destroyItem(ViewGroup container, int position, Object object) {
8592 @ Override
8693 public void startUpdate (ViewGroup container ) {
8794 super .startUpdate (container );
95+ final int realCount = getRealCount ();
96+ if (realCount == 0 ) {
97+ return ;
98+ }
8899 if (mIsLoop ) {
89100 CardViewPager viewPager = (CardViewPager ) container ;
90101 int position = viewPager .getCurrentItem ();
91102 if (position == 0 ) {
92- position = getFristItem ( );
103+ position = getFirstItem ( realCount );
93104 } else if (position == getCount () - 1 ) {
94- final int realCount = getRealCount ();
95- position = getLastItem (position % realCount );
105+ position = getLastItem (realCount , position % realCount );
96106 }
97107 viewPager .setCurrentItem (position , false );
98108 }
@@ -102,13 +112,11 @@ private int getRealCount() {
102112 return mCardItems == null ? 0 : mCardItems .size ();
103113 }
104114
105- private int getFristItem () {
106- int realCount = getRealCount ();
115+ private int getFirstItem (int realCount ) {
107116 return MAX_VALUE / realCount / 2 * realCount ;
108117 }
109118
110- private int getLastItem (int index ) {
111- int realCount = getRealCount ();
119+ private int getLastItem (int realCount , int index ) {
112120 return MAX_VALUE / realCount / 2 * realCount + index ;
113121 }
114122}
0 commit comments