From 6703c4a37495a7a0dc6ec36f0168075d505bdea7 Mon Sep 17 00:00:00 2001 From: zhpanvip Date: Thu, 17 Jun 2021 10:56:13 +0800 Subject: [PATCH] stopLoopWhenDetachedFromWindow supported for issue #207 --- .../zhpan/banner/fragment/HomeFragment.java | 1 + .../com/zhpan/bannerview/BannerViewPager.java | 21 +++++++++++++++++-- .../bannerview/manager/BannerOptions.java | 10 +++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) 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 c911b2d..7cde800 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,6 +189,7 @@ private void initVerticalBanner() { mViewPagerVertical .setAutoPlay(true) .setScrollDuration(500) + .stopLoopWhenDetachedFromWindow(false) .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 56f797f..5ab0ce7 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java @@ -137,14 +137,20 @@ private void initView() { @Override protected void onDetachedFromWindow() { - stopLoop(); + if (mBannerManager != null && mBannerManager.getBannerOptions() + .isStopLoopWhenDetachedFromWindow()) { + stopLoop(); + } super.onDetachedFromWindow(); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); - startLoop(); + if (mBannerManager != null && mBannerManager.getBannerOptions() + .isStopLoopWhenDetachedFromWindow()) { + startLoop(); + } } @Override @@ -1087,6 +1093,17 @@ public BannerViewPager setRTLMode(boolean rtlMode) { return this; } + /** + * @param stopLoopWhenDetachedFromWindow 当BVP滑动出屏幕的时候是否要停止轮播, + * + * true:滑动出屏幕停止自动轮播,false:滑动出屏幕继续自动轮播。默认值为true + */ + public BannerViewPager stopLoopWhenDetachedFromWindow(boolean stopLoopWhenDetachedFromWindow) { + mBannerManager.getBannerOptions() + .setStopLoopWhenDetachedFromWindow(stopLoopWhenDetachedFromWindow); + return this; + } + /** * @deprecated Use {@link BannerViewPager#disallowParentInterceptDownEvent(boolean)} instead. */ diff --git a/bannerview/src/main/java/com/zhpan/bannerview/manager/BannerOptions.java b/bannerview/src/main/java/com/zhpan/bannerview/manager/BannerOptions.java index 2f7caa2..e17d877 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/manager/BannerOptions.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/manager/BannerOptions.java @@ -70,6 +70,8 @@ public BannerOptions() { private boolean disallowParentInterceptDownEvent; + private boolean stopLoopWhenDetachedFromWindow = true; + private final IndicatorOptions mIndicatorOptions; public int getInterval() { @@ -296,6 +298,14 @@ public void setRtl(boolean rtl) { rtl ? IndicatorOrientation.INDICATOR_RTL : IndicatorOrientation.INDICATOR_HORIZONTAL); } + public boolean isStopLoopWhenDetachedFromWindow() { + return stopLoopWhenDetachedFromWindow; + } + + public void setStopLoopWhenDetachedFromWindow(boolean stopLoopWhenDetachedFromWindow) { + this.stopLoopWhenDetachedFromWindow = stopLoopWhenDetachedFromWindow; + } + public static class IndicatorMargin { private final int left, right, top, bottom;