Skip to content

Commit

Permalink
Fix bug #31 (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip authored Nov 19, 2019
1 parent 2a8f035 commit 544676a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,16 @@ public void onClick(View view) {
}

@Override
public void onDestroyView() {
public void onStop() {
if (mViewPager != null)
mViewPager.stopLoop();
super.onStop();
}

@Override
public void onResume() {
super.onResume();
if (mViewPager != null)
mViewPager.stopLoop();
super.onDestroyView();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ public void onStop() {

@Override
public void onResume() {

super.onResume();
if (mViewPager != null)
mViewPager.startLoop();
super.onResume();
}
}
33 changes: 7 additions & 26 deletions bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,10 @@ private void setupViewPager() {
removeAllViews();
BannerPagerAdapter<T, VH> bannerPagerAdapter =
new BannerPagerAdapter<>(mList, holderCreator);
bannerPagerAdapter.setPageStyle(mPageStyle);
bannerPagerAdapter.setCanLoop(isCanLoop);
bannerPagerAdapter.setPageClickListener(position -> {
if (mOnPageClickListener != null) {
mOnPageClickListener.onPageClick(PositionUtils.getRealPosition(isCanLoop, position, mList.size(), mPageStyle));
mOnPageClickListener.onPageClick(position);
}
});

Expand Down Expand Up @@ -356,12 +355,12 @@ private void setMultiPageStyle(boolean overlap, float scale) {

@Override
public void onPageSelected(int position) {
currentPosition = PositionUtils.getRealPosition(isCanLoop, position, mList.size());
if (mOnPageChangeListener != null)
mOnPageChangeListener.onPageSelected(PositionUtils.getRealPosition(isCanLoop, position, mList.size()));
mOnPageChangeListener.onPageSelected(currentPosition);
if (mIndicatorView != null) {
mIndicatorView.onPageSelected(PositionUtils.getRealPosition(isCanLoop, position, mList.size()));
mIndicatorView.onPageSelected(currentPosition);
}
currentPosition = position;
}

@Override
Expand All @@ -372,24 +371,6 @@ public void onPageScrollStateChanged(int state) {
if (mOnPageChangeListener != null) {
mOnPageChangeListener.onPageScrollStateChanged(state);
}
// if (isCanLoop) {
// switch (state) {
// case ViewPager.SCROLL_STATE_IDLE:
// if (currentPosition == 0) {
// mViewPager.setCurrentItem(mList.size(), false);
// } else if (currentPosition == mList.size() + 1) {
// mViewPager.setCurrentItem(1, false);
// }
// break;
// case ViewPager.SCROLL_STATE_DRAGGING:
// if (currentPosition == mList.size() + 1) {
// mViewPager.setCurrentItem(1, false);
// } else if (currentPosition == 0) {
// mViewPager.setCurrentItem(mList.size(), false);
// }
// break;
// }
// }
}

@Override
Expand Down Expand Up @@ -679,7 +660,7 @@ public void create(List<T> list) {
* @return the currently selected page position.
*/
public int getCurrentItem() {
return PositionUtils.getRealPosition(isCanLoop, currentPosition, mList.size(), mPageStyle);
return currentPosition;
}

/**
Expand All @@ -690,7 +671,7 @@ public int getCurrentItem() {
* @param item Item index to select
*/
public void setCurrentItem(int item) {
mViewPager.setCurrentItem(PositionUtils.toUnrealPosition(isCanLoop, item, mList.size(), mPageStyle));
mViewPager.setCurrentItem(isCanLoop ? MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1 + item : item);
}

/**
Expand All @@ -700,7 +681,7 @@ public void setCurrentItem(int item) {
* @param smoothScroll True to smoothly scroll to the new item, false to transition immediately
*/
public void setCurrentItem(int item, boolean smoothScroll) {
mViewPager.setCurrentItem(PositionUtils.toUnrealPosition(isCanLoop, item, mList.size(), mPageStyle), smoothScroll);
mViewPager.setCurrentItem(isCanLoop ? MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1 + item : item, smoothScroll);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.view.View;
import android.view.ViewGroup;

import com.zhpan.bannerview.annotation.APageStyle;
import com.zhpan.bannerview.holder.HolderCreator;
import com.zhpan.bannerview.holder.ViewHolder;
import com.zhpan.bannerview.utils.PositionUtils;
Expand All @@ -30,8 +29,6 @@ public class BannerPagerAdapter<T, VH extends ViewHolder> extends PagerAdapter {

private List<View> mViewList = new ArrayList<>();

private int mPageStyle;

public static final int MAX_VALUE = Integer.MAX_VALUE;

public BannerPagerAdapter(List<T> list, HolderCreator<VH> holderCreator) {
Expand Down Expand Up @@ -77,7 +74,7 @@ private View findViewByPosition(ViewGroup container, int position) {
private View getView(final int position, ViewGroup container) {
ViewHolder<T> holder = holderCreator.createViewHolder();
if (holder == null) {
throw new RuntimeException("can not return a null holder");
throw new NullPointerException("can not return a null holder");
}
return createView(holder, position, container);
}
Expand All @@ -96,7 +93,7 @@ private void setViewListener(View view, int position) {
if (view != null)
view.setOnClickListener(v -> {
if (null != mPageClickListener)
mPageClickListener.onPageClick(PositionUtils.toUnrealPosition(isCanLoop, position, mList.size(), mPageStyle));
mPageClickListener.onPageClick(position);
});
}

Expand All @@ -118,9 +115,6 @@ public void setCanLoop(boolean canLoop) {
isCanLoop = canLoop;
}

public void setPageStyle(@APageStyle int pageStyle) {
mPageStyle = pageStyle;
}

public interface PageClickListener {
void onPageClick(int position);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,8 @@
package com.zhpan.bannerview.utils;

import com.zhpan.bannerview.constants.PageStyle;

public class PositionUtils {

public static int toUnrealPosition(boolean isCanLoop, int position, int pageSize, int pageStyle) {
if (isCanLoop) {
if (pageStyle == PageStyle.NORMAL) {
return (position < pageSize) ? (++position) : pageSize;
} else {
return (position < pageSize) ? position + 2 : pageSize + 1;
}
} else {
return position;
}
}

public static int getRealPosition(boolean isCanLoop, int position, int pageSize) {
int realPosition;
if (pageSize <= 0)
return 0;
if (isCanLoop) {
realPosition = (position - 1 + pageSize) % pageSize;
} else {
realPosition = (position + pageSize) % pageSize;
}
if (realPosition < 0)
realPosition += pageSize;
return realPosition;
return isCanLoop ? (position - 1 + pageSize) % pageSize : (position + pageSize) % pageSize;
}

public static int getRealPosition(boolean isCanLoop, int position, int pageSize, int pageStyle) {
if (isCanLoop) {
if (pageStyle == PageStyle.NORMAL) {
if (position == 0) {
return pageSize - 1;
} else if (position == pageSize + 1) {
return 0;
} else {
return --position;
}
} else {
if (position == 0) {
return pageSize == 1 ? 0 : pageSize - 2;
} else if (position == 1) {
return pageSize - 1;
} else if (position == pageSize + 3) {
return 1;
} else if (position == pageSize + 2) {
return 0;
} else {
return position - 2;
}
}

} else {
return position;
}
}

}

0 comments on commit 544676a

Please sign in to comment.