From 544676aa5a42210112974b2d32d2d9136a4973e6 Mon Sep 17 00:00:00 2001 From: zhpanvip Date: Tue, 19 Nov 2019 12:54:36 +0800 Subject: [PATCH] Fix bug #31 (#32) --- .../fragment/IndicatorFragment.java | 11 +++- .../fragment/PageFragment.java | 3 +- .../com/zhpan/bannerview/BannerViewPager.java | 33 +++-------- .../adapter/BannerPagerAdapter.java | 10 +--- .../zhpan/bannerview/utils/PositionUtils.java | 56 +------------------ 5 files changed, 20 insertions(+), 93 deletions(-) diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/fragment/IndicatorFragment.java b/app/src/main/java/com/example/zhpan/circleviewpager/fragment/IndicatorFragment.java index 9bbe57c1..61480c67 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/fragment/IndicatorFragment.java +++ b/app/src/main/java/com/example/zhpan/circleviewpager/fragment/IndicatorFragment.java @@ -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(); } } diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/fragment/PageFragment.java b/app/src/main/java/com/example/zhpan/circleviewpager/fragment/PageFragment.java index 19718949..439f03af 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/fragment/PageFragment.java +++ b/app/src/main/java/com/example/zhpan/circleviewpager/fragment/PageFragment.java @@ -115,9 +115,8 @@ public void onStop() { @Override public void onResume() { - + super.onResume(); if (mViewPager != null) mViewPager.startLoop(); - super.onResume(); } } diff --git a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java index be5cb57e..2a5ce0a2 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java @@ -299,11 +299,10 @@ private void setupViewPager() { removeAllViews(); BannerPagerAdapter 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); } }); @@ -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 @@ -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 @@ -679,7 +660,7 @@ public void create(List list) { * @return the currently selected page position. */ public int getCurrentItem() { - return PositionUtils.getRealPosition(isCanLoop, currentPosition, mList.size(), mPageStyle); + return currentPosition; } /** @@ -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); } /** @@ -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); } /** diff --git a/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java b/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java index 0566fe48..0c645f56 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java @@ -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; @@ -30,8 +29,6 @@ public class BannerPagerAdapter extends PagerAdapter { private List mViewList = new ArrayList<>(); - private int mPageStyle; - public static final int MAX_VALUE = Integer.MAX_VALUE; public BannerPagerAdapter(List list, HolderCreator holderCreator) { @@ -77,7 +74,7 @@ private View findViewByPosition(ViewGroup container, int position) { private View getView(final int position, ViewGroup container) { ViewHolder 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); } @@ -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); }); } @@ -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); diff --git a/bannerview/src/main/java/com/zhpan/bannerview/utils/PositionUtils.java b/bannerview/src/main/java/com/zhpan/bannerview/utils/PositionUtils.java index 70f3a301..0b6c5f68 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/utils/PositionUtils.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/utils/PositionUtils.java @@ -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; - } - } - }