Skip to content

Commit

Permalink
Merge pull request #141 from zhpanvip/bug_115
Browse files Browse the repository at this point in the history
Fix Issue #115 and #140
  • Loading branch information
zhpanvip authored Jul 12, 2020
2 parents 4e04a46 + 3d6896c commit c139e8a
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 80 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ It's also support to custom indicator style,just need extends BaseIndicatorView
| BannerViewPager<T, VH> setRoundRect(int) | set round corner for BVP| Require SDK_INT>=LOLLIPOP |
| BannerViewPager<T, VH> setOrientation(int) | Sets the orientation of the BVP| support in v3.0.0 {@link #ORIENTATION_HORIZONTAL} or {@link #ORIENTATION_VERTICAL} |
| BannerViewPager<T, VH> setUserInputEnabled(int) | Enable or disable user initiated scrolling | |
| BannerViewPager<T, VH> setLifecycleRegistry(Lifecycle) | Add lifecycle for BVP, | |
| void startLoop() |start loop | Don't need call this method while init BVP |
| void stopLoop() | Stop loop | |
| List\<T> getData() | return data in BVP | |
Expand Down Expand Up @@ -266,6 +267,7 @@ public class HomeAdapter extends BaseBannerAdapter<BannerData, NetViewHolder> {
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))
Expand Down Expand Up @@ -295,6 +297,7 @@ public class HomeAdapter extends BaseBannerAdapter<BannerData, NetViewHolder> {
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())
Expand Down Expand Up @@ -350,11 +353,14 @@ If fetching data synchronously,you can call create(List<T>) 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)
Expand Down
24 changes: 13 additions & 11 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ BannerViewPager目前已支持三种IndicatorViewStyle,以及五种IndicatorSlid
| BannerViewPager<T, VH> setRoundRect(int) | 设置页面滑动方向|为BannerViewPager设置圆角 |
| BannerViewPager<T, VH> setOrientation(int) | 设置页面滑动方向| 3.0.0新增 支持水平和竖直滑动 |
| BannerViewPager<T, VH> setUserInputEnabled(int) | 是否开启用户输入 | |
| BannerViewPager<T, VH> setLifecycleRegistry(Lifecycle) | 为BVP设置Lifecycle | |
| void startLoop() |开启自动轮播 | 初始化BannerViewPager时不必调用该方法,设置setAutoPlay后会调用startLoop() |
| void stopLoop() | 停止自动轮播 | |
| List\<T> getData() | 获取Banner中的集合数据 | |
Expand Down Expand Up @@ -265,6 +266,7 @@ public class HomeAdapter extends BaseBannerAdapter<BannerData, NetViewHolder> {
.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))
Expand Down Expand Up @@ -293,6 +295,7 @@ public class HomeAdapter extends BaseBannerAdapter<BannerData, NetViewHolder> {
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())
Expand Down Expand Up @@ -344,21 +347,20 @@ public class HomeAdapter extends BaseBannerAdapter<BannerData, NetViewHolder> {
}
```


### 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArticleWrapper.Article>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class ArticleAdapter(val context: Context, data: List<ArticleWrapper.Article>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val mList = ArrayList<ArticleWrapper.Article>()
private val inflater: LayoutInflater

Expand All @@ -45,7 +44,7 @@ class ArticleAdapter(context: Context, data: List<ArticleWrapper.Article>) : 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
Expand Down Expand Up @@ -76,6 +75,10 @@ class ArticleAdapter(context: Context, data: List<ArticleWrapper.Article>) : Rec
var bannerViewPager: BannerViewPager<Int, ImageResourceViewHolder> = itemView.findViewById(R.id.banner_view3)
var resources: Resources = itemView.context.resources

init {
if (context is AppCompatActivity)
bannerViewPager.setLifecycleRegistry(context.lifecycle)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand Down Expand Up @@ -167,6 +155,7 @@ private void initBanner() {
HomeAdapter homeAdapter = new HomeAdapter();
mViewPagerHorizontal
.setScrollDuration(600)
.setLifecycleRegistry(getLifecycle())
.setIndicatorStyle(IndicatorStyle.CIRCLE)
.setIndicatorSlideMode(IndicatorSlideMode.WORM)
.setInterval(3000)
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<Integer, ImageResourceViewHolder>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
22 changes: 20 additions & 2 deletions bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +50,7 @@
/**
* Created by zhpan on 2017/3/28.
*/
public class BannerViewPager<T, VH extends BaseViewHolder<T>> extends RelativeLayout {
public class BannerViewPager<T, VH extends BaseViewHolder<T>> extends RelativeLayout implements LifecycleObserver {

private int currentPosition;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -925,4 +928,19 @@ public BannerViewPager<T, VH> disableTouchScroll(boolean disableTouchScroll) {
return this;
}

public BannerViewPager<T, VH> 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();
}

}

0 comments on commit c139e8a

Please sign in to comment.