Skip to content

Commit

Permalink
Fix #295 从别的页面返回到展示Banner的页面时,不会自动恢复轮播
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip committed Jan 19, 2023
1 parent 9d57c48 commit c981d90
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser
private Path mRadiusPath;

private int startX, startY;
private Lifecycle lifecycleRegistry;

private final ViewPager2.OnPageChangeCallback mOnPageChangeCallback =
new ViewPager2.OnPageChangeCallback() {
Expand Down Expand Up @@ -513,8 +514,13 @@ public List<T> 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;
}
Expand Down Expand Up @@ -1067,12 +1073,13 @@ public BannerViewPager<T> registerOnPageChangeCallback(
*/
@Deprecated
public BannerViewPager<T> setLifecycleRegistry(Lifecycle lifecycleRegistry) {
lifecycleRegistry.addObserver(this);
registerLifecycleObserver(lifecycleRegistry);
return this;
}

public BannerViewPager<T> registerLifecycleObserver(Lifecycle lifecycleRegistry) {
lifecycleRegistry.addObserver(this);
this.lifecycleRegistry = lifecycleRegistry;
return this;
}

Expand All @@ -1088,9 +1095,7 @@ public void onPause() {

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
if (!isStopLoopWhenDetachedFromWindow()) {
startLoop();
}
startLoop();
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
Expand Down

0 comments on commit c981d90

Please sign in to comment.