diff --git a/app/src/main/java/com/example/zhpan/banner/fragment/HomeFragment.java b/app/src/main/java/com/example/zhpan/banner/fragment/HomeFragment.java index b962012..5d8de5f 100644 --- a/app/src/main/java/com/example/zhpan/banner/fragment/HomeFragment.java +++ b/app/src/main/java/com/example/zhpan/banner/fragment/HomeFragment.java @@ -189,7 +189,7 @@ private void initVerticalBanner() { mViewPagerVertical .setAutoPlay(true) .setScrollDuration(500) - .stopLoopWhenDetachedFromWindow(false) + .stopLoopWhenDetachedFromWindow(true) .setLifecycleRegistry(getLifecycle()) .setIndicatorStyle(IndicatorStyle.ROUND_RECT) .setIndicatorSlideMode(IndicatorSlideMode.SCALE) diff --git a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java index a840721..88461e5 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java @@ -134,8 +134,7 @@ private void initView() { @Override protected void onDetachedFromWindow() { - if (mBannerManager != null && mBannerManager.getBannerOptions() - .isStopLoopWhenDetachedFromWindow()) { + if (mBannerManager != null && isStopLoopWhenDetachedFromWindow()) { stopLoop(); } super.onDetachedFromWindow(); @@ -144,8 +143,7 @@ protected void onDetachedFromWindow() { @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); - if (mBannerManager != null && mBannerManager.getBannerOptions() - .isStopLoopWhenDetachedFromWindow()) { + if (mBannerManager != null && isStopLoopWhenDetachedFromWindow()) { startLoop(); } } @@ -353,6 +351,10 @@ private void initIndicatorSliderMargin() { } } + private boolean isStopLoopWhenDetachedFromWindow() { + return mBannerManager.getBannerOptions().isStopLoopWhenDetachedFromWindow(); + } + @Override protected void dispatchDraw(Canvas canvas) { float[] roundRectRadiusArray = mBannerManager.getBannerOptions().getRoundRectRadiusArray(); @@ -505,7 +507,7 @@ public void startLoop() { */ public void startLoopNow() { if (!isLooping && isAutoPlay() && mBannerPagerAdapter != null && - mBannerPagerAdapter.getListSize() > 1) { + mBannerPagerAdapter.getListSize() > 1) { mHandler.post(mRunnable); isLooping = true; } @@ -1051,7 +1053,9 @@ public void onPause() { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public void onResume() { - startLoop(); + if (isAttachedToWindow() || !isStopLoopWhenDetachedFromWindow()) { + startLoop(); + } } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)