diff --git a/README.md b/README.md index 68d7f31b..ec82f0b5 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,7 @@ It's also support to custom indicator style,just need extends BaseIndicatorView | BannerViewPager setRoundRect(int) | set round corner for BVP| Require SDK_INT>=LOLLIPOP | | BannerViewPager setOrientation(int) | Sets the orientation of the BVP| support in v3.0.0 {@link #ORIENTATION_HORIZONTAL} or {@link #ORIENTATION_VERTICAL} | | BannerViewPager setUserInputEnabled(int) | Enable or disable user initiated scrolling | | +| BannerViewPager setLifecycleRegistry(Lifecycle) | Add lifecycle for BVP, | | | void startLoop() |start loop | Don't need call this method while init BVP | | void stopLoop() | Stop loop | | | List\ getData() | return data in BVP | | @@ -266,6 +267,7 @@ public class HomeAdapter extends BaseBannerAdapter { mViewPager .setAutoPlay(true) .setScrollDuration(800) + .setLifecycleRegistry(getLifecycle()) .setIndicatorStyle(IndicatorStyle.ROUND_RECT) .setIndicatorSliderGap(getResources().getDimensionPixelOffset(R.dimen.dp_4)) .setIndicatorSliderWidth(getResources().getDimensionPixelOffset(R.dimen.dp_4), getResources().getDimensionPixelOffset(R.dimen.dp_10)) @@ -295,6 +297,7 @@ public class HomeAdapter extends BaseBannerAdapter { mViewPager.apply { adapter = HomeAdapter() setAutoPlay(true) + setLifecycleRegistry(lifecycle) setIndicatorStyle(IndicatorStyle.ROUND_RECT) setIndicatorSliderGap(getResources().getDimensionPixelOffset(R.dimen.dp_4)) setIndicatorMargin(0, 0, 0, resources.getDimension(R.dimen.dp_100).toInt()) @@ -350,11 +353,14 @@ If fetching data synchronously,you can call create(List) method with parame ### 6.startLoop and stopLoop -***If the version you used is later than 2.5.0,you don't need care of startLoop and stopLoop in Activity or Fragment. But the two methods is still public.*** - -Recommend call stopLoop in onPause() and startLoop in onResume() to improve performance: +***You can user setLifecycleRegistry(Lifecycle) method to instead of call stopLoop and startLoop in Activity or Fragment*** ``` + + mViewPager.setLifecycleRegistry(getLifecycle()) + + // the follwoing code is deprecated. + @Override protected void onPause() { if (mBannerViewPager != null) diff --git a/README_CN.md b/README_CN.md index 9a6237a7..2ada2c60 100644 --- a/README_CN.md +++ b/README_CN.md @@ -106,6 +106,7 @@ BannerViewPager目前已支持三种IndicatorViewStyle,以及五种IndicatorSlid | BannerViewPager setRoundRect(int) | 设置页面滑动方向|为BannerViewPager设置圆角 | | BannerViewPager setOrientation(int) | 设置页面滑动方向| 3.0.0新增 支持水平和竖直滑动 | | BannerViewPager setUserInputEnabled(int) | 是否开启用户输入 | | +| BannerViewPager setLifecycleRegistry(Lifecycle) | 为BVP设置Lifecycle | | | void startLoop() |开启自动轮播 | 初始化BannerViewPager时不必调用该方法,设置setAutoPlay后会调用startLoop() | | void stopLoop() | 停止自动轮播 | | | List\ getData() | 获取Banner中的集合数据 | | @@ -265,6 +266,7 @@ public class HomeAdapter extends BaseBannerAdapter { .setAutoPlay(true) .setScrollDuration(800) .setIndicatorStyle(IndicatorStyle.ROUND_RECT) + .setLifecycleRegistry(getLifecycle()) .setIndicatorSliderGap(getResources().getDimensionPixelOffset(R.dimen.dp_4)) .setIndicatorSliderWidth(getResources().getDimensionPixelOffset(R.dimen.dp_4), getResources().getDimensionPixelOffset(R.dimen.dp_10)) .setIndicatorSliderColor(getColor(R.color.red_normal_color), getColor(R.color.red_checked_color)) @@ -293,6 +295,7 @@ public class HomeAdapter extends BaseBannerAdapter { mViewPager.apply { adapter = HomeAdapter() setAutoPlay(true) + setLifecycleRegistry(lifecycle) setIndicatorStyle(IndicatorStyle.ROUND_RECT) setIndicatorSliderGap(getResources().getDimensionPixelOffset(R.dimen.dp_4)) setIndicatorMargin(0, 0, 0, resources.getDimension(R.dimen.dp_100).toInt()) @@ -344,21 +347,20 @@ public class HomeAdapter extends BaseBannerAdapter { } ``` - ### 6.开启与停止轮播 -***2.5.0之后版本无需自行在Activity或Fragment中管理stopLoop和startLoop方法,但这两个方法依旧保留对外开放*** - -但是为了节省性能建议在onPause中停止轮播,在onResume中开启轮播: +***使用setLifecycleRegistry(getLifecycle())方法替代在Activity或Fragment中调用startLoop和stopLoop*** ``` - @Override - public void onPause() { - super.onPause(); - if (mViewPager != null) { - mViewPager.stopLoop(); - } - } + mViewPager.setLifecycleRegistry(getLifecycle()) + + // 下边代码已过时 + @Override + protected void onPause() { + if (mBannerViewPager != null) + mBannerViewPager.stopLoop(); + super.onPause(); + } @Override protected void onResume() { diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/adapter/ArticleAdapter.kt b/app/src/main/java/com/example/zhpan/circleviewpager/adapter/ArticleAdapter.kt index 11f0a029..ae0a427a 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/adapter/ArticleAdapter.kt +++ b/app/src/main/java/com/example/zhpan/circleviewpager/adapter/ArticleAdapter.kt @@ -7,19 +7,18 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity import androidx.viewpager2.widget.ViewPager2 import com.example.zhpan.circleviewpager.R import com.example.zhpan.circleviewpager.bean.ArticleWrapper -import com.example.zhpan.circleviewpager.bean.CustomBean -import com.example.zhpan.circleviewpager.viewholder.CustomPageViewHolder import com.example.zhpan.circleviewpager.viewholder.ImageResourceViewHolder import com.zhpan.bannerview.BannerViewPager import com.zhpan.indicator.enums.IndicatorStyle import java.util.ArrayList -class ArticleAdapter(context: Context, data: List) : RecyclerView.Adapter() { +class ArticleAdapter(val context: Context, data: List) : RecyclerView.Adapter() { private val mList = ArrayList() private val inflater: LayoutInflater @@ -45,7 +44,7 @@ class ArticleAdapter(context: Context, data: List) : Rec .setIndicatorSliderColor(holder.resources.getColor(R.color.red_normal_color), holder.resources.getColor(R.color.red_checked_color)) .setOrientation(ViewPager2.ORIENTATION_VERTICAL) .setInterval(2000) - .setAdapter(ImageResourceAdapter(0)).create(article.pagers); + .setAdapter(ImageResourceAdapter(0)).create(article.pagers) } else if (holder is ArticleViewHolder) { holder.tvAuthor.text = article.author holder.tvTitle.text = article.title @@ -76,6 +75,10 @@ class ArticleAdapter(context: Context, data: List) : Rec var bannerViewPager: BannerViewPager = itemView.findViewById(R.id.banner_view3) var resources: Resources = itemView.context.resources + init { + if (context is AppCompatActivity) + bannerViewPager.setLifecycleRegistry(context.lifecycle) + } } } diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/fragment/HomeFragment.java b/app/src/main/java/com/example/zhpan/circleviewpager/fragment/HomeFragment.java index 0e65c703..f3cbda09 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/fragment/HomeFragment.java +++ b/app/src/main/java/com/example/zhpan/circleviewpager/fragment/HomeFragment.java @@ -71,24 +71,12 @@ protected void initTitle() { public void onPause() { super.onPause(); LogUtils.e("HomeFragment", "onPause"); - if (mViewPagerHorizontal != null) { - mViewPagerHorizontal.stopLoop(); - } - if (mViewPagerVertical != null) { - mViewPagerVertical.stopLoop(); - } } @Override public void onResume() { super.onResume(); LogUtils.e("HomeFragment", "onResume"); - if (mViewPagerHorizontal != null) { - mViewPagerHorizontal.startLoop(); - } - if (mViewPagerVertical != null) { - mViewPagerVertical.startLoop(); - } } @Override @@ -109,7 +97,7 @@ private void initRecyclerView(View view) { recyclerView.addHeadView(getHeaderView(), true); recyclerView.addItemDecoration(new DividerItemDecoration(getMContext(), DividerItemDecoration.VERTICAL)); - articleAdapter = new ArticleAdapter(getMContext(), new ArrayList<>()); + articleAdapter = new ArticleAdapter(getActivity(), new ArrayList<>()); recyclerView.setAdapter(articleAdapter); recyclerView.getHeadAndFootAdapter(); } @@ -167,6 +155,7 @@ private void initBanner() { HomeAdapter homeAdapter = new HomeAdapter(); mViewPagerHorizontal .setScrollDuration(600) + .setLifecycleRegistry(getLifecycle()) .setIndicatorStyle(IndicatorStyle.CIRCLE) .setIndicatorSlideMode(IndicatorSlideMode.WORM) .setInterval(3000) @@ -188,6 +177,7 @@ public void onPageSelected(int position) { mViewPagerVertical .setAutoPlay(true) .setScrollDuration(500) + .setLifecycleRegistry(getLifecycle()) .setIndicatorStyle(IndicatorStyle.ROUND_RECT) .setIndicatorSlideMode(IndicatorSlideMode.SCALE) .setIndicatorSliderGap(getResources().getDimensionPixelOffset(R.dimen.dp_4)) 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 7ac10149..d318de12 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 @@ -43,21 +43,6 @@ protected void initTitle() { } - @Override - public void onPause() { - super.onPause(); - if (mViewPager != null) { - mViewPager.stopLoop(); - } - } - - @Override - public void onResume() { - super.onResume(); - if (mViewPager != null) { - mViewPager.startLoop(); - } - } @Override protected void initView(Bundle savedInstanceState, View view) { @@ -67,6 +52,7 @@ protected void initView(Bundle savedInstanceState, View view) { mViewPager = view.findViewById(R.id.banner_view); mViewPager.setIndicatorSliderGap(BannerUtils.dp2px(6)) .setScrollDuration(800) + .setLifecycleRegistry(getLifecycle()) .setIndicatorGravity(IndicatorGravity.CENTER) .setOnPageClickListener(position -> ToastUtils.show("position:" + position)) .setAdapter(new BaseBannerAdapter() { diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/fragment/OthersFragment.java b/app/src/main/java/com/example/zhpan/circleviewpager/fragment/OthersFragment.java index f9b7a14a..c6927aee 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/fragment/OthersFragment.java +++ b/app/src/main/java/com/example/zhpan/circleviewpager/fragment/OthersFragment.java @@ -60,22 +60,6 @@ private void initRefreshLayout(View view) { }); } - @Override - public void onPause() { - super.onPause(); - if (mViewPager != null) { - mViewPager.stopLoop(); - } - } - - @Override - public void onResume() { - super.onResume(); - if (mViewPager != null) { - mViewPager.startLoop(); - } - } - @Override protected void initView(Bundle savedInstanceState, @NonNull View view) { initRefreshLayout(view); @@ -86,6 +70,7 @@ protected void initView(Bundle savedInstanceState, @NonNull View view) { view.findViewById(R.id.tv_photo_view).setOnClickListener(this); mViewPager.setIndicatorSliderGap(BannerUtils.dp2px(6)) .setIndicatorView(mIndicatorView) + .setLifecycleRegistry(getLifecycle()) .setRoundCorner(BannerUtils.dp2px(6)) .setOnPageClickListener(position -> { ToastUtils.show("position:" + position); 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 60aa44a3..4e09b238 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 @@ -37,22 +37,6 @@ protected void initTitle() { } - @Override - public void onPause() { - super.onPause(); - if (mViewPager != null) { - mViewPager.stopLoop(); - } - } - - @Override - public void onResume() { - super.onResume(); - if (mViewPager != null) { - mViewPager.startLoop(); - } - } - @Override protected void initView(Bundle savedInstanceState, View view) { mViewPager = view.findViewById(R.id.banner_view); @@ -61,6 +45,7 @@ protected void initView(Bundle savedInstanceState, View view) { .setIndicatorSlideMode(IndicatorSlideMode.SCALE) .setIndicatorSliderColor(getColor(R.color.red_normal_color), getColor(R.color.red_checked_color)) .setIndicatorSliderRadius(getResources().getDimensionPixelOffset(R.dimen.dp_4), getResources().getDimensionPixelOffset(R.dimen.dp_5)) + .setLifecycleRegistry(getLifecycle()) .setOnPageClickListener(this::pageClick) .setAdapter(new ImageResourceAdapter(getResources().getDimensionPixelOffset(R.dimen.dp_8))) .setInterval(5000); diff --git a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java index e5f62dc4..675aef63 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java @@ -6,6 +6,10 @@ import androidx.annotation.ColorInt; import androidx.annotation.Nullable; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LifecycleObserver; +import androidx.lifecycle.LifecycleRegistry; +import androidx.lifecycle.OnLifecycleEvent; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager2.widget.CompositePageTransformer; import androidx.viewpager2.widget.MarginPageTransformer; @@ -46,7 +50,7 @@ /** * Created by zhpan on 2017/3/28. */ -public class BannerViewPager> extends RelativeLayout { +public class BannerViewPager> extends RelativeLayout implements LifecycleObserver { private int currentPosition; @@ -197,7 +201,6 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { case MotionEvent.ACTION_DOWN: startX = (int) ev.getX(); startY = (int) ev.getY(); - getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: int endX = (int) ev.getX(); @@ -925,4 +928,19 @@ public BannerViewPager disableTouchScroll(boolean disableTouchScroll) { return this; } + public BannerViewPager setLifecycleRegistry(Lifecycle lifecycleRegistry) { + lifecycleRegistry.addObserver(this); + return this; + } + + @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) + public void onPause() { + stopLoop(); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) + public void onResume() { + startLoop(); + } + }