Skip to content

Commit

Permalink
* [android] slider: fix the IllegalStateException (#58)
Browse files Browse the repository at this point in the history
* * [android] fix and prevent the IllegalStateException
(cherry picked from commit 06bdf78)

* * [android] fix and prevent the IllegalStateException
(cherry picked from commit 06bdf78)
  • Loading branch information
misakuo authored and sospartan committed Dec 7, 2016
1 parent 5536be6 commit 0e94762
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Expand Up @@ -325,16 +325,19 @@ public List<View> getViews(){
}

private void ensureShadow() {
shadow.clear();
List<View> temp = new ArrayList<>();
if (needLoop && views.size() > 2) {
shadow.add(0, views.get(views.size() - 1));
temp.add(0, views.get(views.size() - 1));
for (View view : views) {
shadow.add(view);
temp.add(view);
}
shadow.add(views.get(0));
temp.add(views.get(0));
} else {
shadow.addAll(views);
temp.addAll(views);
}
shadow.clear();
notifyDataSetChanged();
shadow.addAll(temp);
notifyDataSetChanged();
}

Expand Down
Expand Up @@ -235,7 +235,7 @@ public class WXCircleViewPager extends ViewPager implements WXGestureObservable
public void run() {
//don't override ViewPager#setCurrentItem(int item, bool smoothScroll)
WXLogUtils.d("[CircleViewPager] trigger auto play action");
setCurrentItem(WXCircleViewPager.super.getCurrentItem()+1, true);
superSetCurrentItem(WXCircleViewPager.super.getCurrentItem()+1, true);
removeCallbacks(this);
postDelayed(this, intervalTime);
}
Expand Down Expand Up @@ -267,9 +267,9 @@ public void onPageScrollStateChanged(int state) {
int currentItemInternal = WXCircleViewPager.super.getCurrentItem();
if (needLoop && state == ViewPager.SCROLL_STATE_IDLE && adapter.getCount() > 1) {
if (currentItemInternal == adapter.getCount() - 1) {
WXCircleViewPager.super.setCurrentItem(1, false);
superSetCurrentItem(1, false);
} else if (currentItemInternal == 0) {
WXCircleViewPager.super.setCurrentItem(adapter.getCount() - 2, false);
superSetCurrentItem(adapter.getCount() - 2, false);
}
}
}
Expand Down Expand Up @@ -419,10 +419,35 @@ public int getRealCurrentItem() {
}

private void setRealCurrentItem(int item) {
super.setCurrentItem(((WXCirclePageAdapter) getAdapter()).getFirst() + item);
superSetCurrentItem(((WXCirclePageAdapter) getAdapter()).getFirst() + item, false);
}

private void superSetCurrentItem(int item, boolean smooth) {
try {
super.setCurrentItem(item, smooth);
} catch (IllegalStateException e) {
WXLogUtils.e(e.toString());
if (getAdapter() != null) {
getAdapter().notifyDataSetChanged();
super.setCurrentItem(item, smooth);
}
}
}

public int getRealCount() {
return ((WXCirclePageAdapter) getAdapter()).getRealCount();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} catch (IllegalStateException e) {
WXLogUtils.e(e.toString());
if (getAdapter() != null) {
getAdapter().notifyDataSetChanged();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
}

0 comments on commit 0e94762

Please sign in to comment.