From c981d90245ead28ed3f59f07a5257251946df248 Mon Sep 17 00:00:00 2001 From: zhpanvip Date: Thu, 19 Jan 2023 18:20:43 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20#295=20=E4=BB=8E=E5=88=AB=E7=9A=84?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E8=BF=94=E5=9B=9E=E5=88=B0=E5=B1=95=E7=A4=BA?= =?UTF-8?q?Banner=E7=9A=84=E9=A1=B5=E9=9D=A2=E6=97=B6=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E4=BC=9A=E8=87=AA=E5=8A=A8=E6=81=A2=E5=A4=8D=E8=BD=AE=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zhpan/bannerview/BannerViewPager.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java index 2e7c732..1db9a2d 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java @@ -99,6 +99,7 @@ public class BannerViewPager extends RelativeLayout implements LifecycleObser private Path mRadiusPath; private int startX, startY; + private Lifecycle lifecycleRegistry; private final ViewPager2.OnPageChangeCallback mOnPageChangeCallback = new ViewPager2.OnPageChangeCallback() { @@ -513,8 +514,13 @@ public List getData() { * Start loop */ public void startLoop() { - if (!isLooping && isAutoPlay() && mBannerPagerAdapter != null && - mBannerPagerAdapter.getListSize() > 1 && isAttachedToWindow()) { + if (!isLooping + && isAutoPlay() + && mBannerPagerAdapter != null + && + mBannerPagerAdapter.getListSize() > 1 + && isAttachedToWindow() + && lifecycleRegistry.getCurrentState() == Lifecycle.State.RESUMED) { mHandler.postDelayed(mRunnable, getInterval()); isLooping = true; } @@ -1067,12 +1073,13 @@ public BannerViewPager registerOnPageChangeCallback( */ @Deprecated public BannerViewPager setLifecycleRegistry(Lifecycle lifecycleRegistry) { - lifecycleRegistry.addObserver(this); + registerLifecycleObserver(lifecycleRegistry); return this; } public BannerViewPager registerLifecycleObserver(Lifecycle lifecycleRegistry) { lifecycleRegistry.addObserver(this); + this.lifecycleRegistry = lifecycleRegistry; return this; } @@ -1088,9 +1095,7 @@ public void onPause() { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public void onResume() { - if (!isStopLoopWhenDetachedFromWindow()) { - startLoop(); - } + startLoop(); } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)