|
@@ -2,13 +2,12 @@ package com.zhpan.bannerview;
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
import android.content.Context;
|
|
|
-import android.content.res.TypedArray;
|
|
|
-import android.graphics.Color;
|
|
|
import android.os.Build;
|
|
|
import android.os.Handler;
|
|
|
|
|
|
import androidx.annotation.ColorInt;
|
|
|
import androidx.annotation.Nullable;
|
|
|
+import androidx.viewpager.widget.PagerAdapter;
|
|
|
import androidx.viewpager.widget.ViewPager;
|
|
|
|
|
|
import android.util.AttributeSet;
|
|
@@ -26,9 +25,10 @@ import com.zhpan.bannerview.annotation.Visibility;
|
|
|
import com.zhpan.bannerview.constants.IndicatorSlideMode;
|
|
|
import com.zhpan.bannerview.constants.IndicatorStyle;
|
|
|
import com.zhpan.bannerview.constants.PageStyle;
|
|
|
-import com.zhpan.bannerview.indicator.DashIndicatorView;
|
|
|
import com.zhpan.bannerview.indicator.IIndicator;
|
|
|
import com.zhpan.bannerview.indicator.IndicatorFactory;
|
|
|
+import com.zhpan.bannerview.manager.BannerManager;
|
|
|
+import com.zhpan.bannerview.manager.BannerOptions;
|
|
|
import com.zhpan.bannerview.transform.pagestyle.ScaleInTransformer;
|
|
|
import com.zhpan.bannerview.utils.BannerUtils;
|
|
|
import com.zhpan.bannerview.adapter.BannerPagerAdapter;
|
|
@@ -36,7 +36,6 @@ import com.zhpan.bannerview.holder.HolderCreator;
|
|
|
import com.zhpan.bannerview.holder.ViewHolder;
|
|
|
import com.zhpan.bannerview.provider.ViewStyleSetter;
|
|
|
import com.zhpan.bannerview.transform.PageTransformerFactory;
|
|
|
-import com.zhpan.bannerview.utils.PositionUtils;
|
|
|
import com.zhpan.bannerview.view.CatchViewPager;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
@@ -55,87 +54,25 @@ import static com.zhpan.bannerview.transform.pagestyle.ScaleInTransformer.MAX_SC
|
|
|
public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout implements
|
|
|
ViewPager.OnPageChangeListener {
|
|
|
|
|
|
- private int interval;
|
|
|
-
|
|
|
private int currentPosition;
|
|
|
|
|
|
- private boolean isLooping;
|
|
|
-
|
|
|
- private boolean isCanLoop;
|
|
|
-
|
|
|
- private boolean isAutoPlay = false;
|
|
|
-
|
|
|
- private int indicatorGravity;
|
|
|
-
|
|
|
- private int indicatorNormalColor;
|
|
|
-
|
|
|
- private int indicatorCheckedColor;
|
|
|
-
|
|
|
- private int normalIndicatorWidth;
|
|
|
-
|
|
|
- private int checkedIndicatorWidth;
|
|
|
-
|
|
|
private OnPageClickListener mOnPageClickListener;
|
|
|
|
|
|
private IIndicator mIndicatorView;
|
|
|
|
|
|
private RelativeLayout mIndicatorLayout;
|
|
|
|
|
|
- private int mPageMargin;
|
|
|
-
|
|
|
- private int mRevealWidth;
|
|
|
-
|
|
|
- private int mIndicatorStyle;
|
|
|
-
|
|
|
- private int mIndicatorSlideMode;
|
|
|
-
|
|
|
private CatchViewPager mViewPager;
|
|
|
|
|
|
private List<T> mList;
|
|
|
|
|
|
- private HolderCreator<VH> holderCreator;
|
|
|
-
|
|
|
- private int indicatorGap;
|
|
|
+ private BannerManager mBannerManager;
|
|
|
|
|
|
- private int indicatorHeight;
|
|
|
-
|
|
|
- private boolean isCustomIndicator;
|
|
|
-
|
|
|
- private int mPageStyle = PageStyle.NORMAL;
|
|
|
-
|
|
|
- private IndicatorMargin mIndicatorMargin;
|
|
|
+ private HolderCreator<VH> holderCreator;
|
|
|
|
|
|
private Handler mHandler = new Handler();
|
|
|
|
|
|
- private Runnable mRunnable = new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- if (mList.size() > 1) {
|
|
|
- currentPosition = mViewPager.getCurrentItem() + 1;
|
|
|
- if (isCanLoop) {
|
|
|
- if (currentPosition == MAX_VALUE - 1) {
|
|
|
- currentPosition = 0;
|
|
|
- mViewPager.setCurrentItem(currentPosition, false);
|
|
|
- mHandler.post(mRunnable);
|
|
|
- } else {
|
|
|
- mViewPager.setCurrentItem(currentPosition);
|
|
|
- mHandler.postDelayed(mRunnable, interval);
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (currentPosition >= MAX_VALUE) {
|
|
|
- stopLoop();
|
|
|
- } else {
|
|
|
- mViewPager.setCurrentItem(currentPosition);
|
|
|
- mHandler.postDelayed(mRunnable, interval);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- private int mIndicatorVisibility;
|
|
|
- private int mScrollDuration;
|
|
|
- private int mRoundCorner;
|
|
|
- private boolean disableTouchScroll;
|
|
|
+ private Runnable mRunnable = this::handlePosition;
|
|
|
|
|
|
public BannerViewPager(Context context) {
|
|
|
this(context, null);
|
|
@@ -147,11 +84,12 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
|
|
|
public BannerViewPager(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
- init(attrs);
|
|
|
+ init(context, attrs);
|
|
|
}
|
|
|
|
|
|
- private void init(AttributeSet attrs) {
|
|
|
- initAttrs(attrs);
|
|
|
+ private void init(Context context, AttributeSet attrs) {
|
|
|
+ mBannerManager = new BannerManager();
|
|
|
+ mBannerManager.initAttrs(context, attrs);
|
|
|
initView();
|
|
|
}
|
|
|
|
|
@@ -162,79 +100,18 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
mList = new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
- private void initAttrs(AttributeSet attrs) {
|
|
|
- if (attrs != null) {
|
|
|
- TypedArray typedArray =
|
|
|
- getContext().obtainStyledAttributes(attrs, R.styleable.BannerViewPager);
|
|
|
-
|
|
|
- interval = typedArray.getInteger(R.styleable.BannerViewPager_bvp_interval, 3000);
|
|
|
- indicatorCheckedColor =
|
|
|
- typedArray.getColor(R.styleable.BannerViewPager_bvp_indicator_checked_color,
|
|
|
- Color.parseColor("#8C18171C"));
|
|
|
- indicatorNormalColor =
|
|
|
- typedArray.getColor(R.styleable.BannerViewPager_bvp_indicator_normal_color,
|
|
|
- Color.parseColor("#8C6C6D72"));
|
|
|
- normalIndicatorWidth = (int) typedArray.getDimension(R.styleable.BannerViewPager_bvp_indicator_radius,
|
|
|
- BannerUtils.dp2px(8));
|
|
|
- isAutoPlay = typedArray.getBoolean(R.styleable.BannerViewPager_bvp_auto_play, true);
|
|
|
- isCanLoop = typedArray.getBoolean(R.styleable.BannerViewPager_bvp_can_loop, true);
|
|
|
- mPageMargin = (int) typedArray.getDimension(R.styleable.BannerViewPager_bvp_page_margin, 0);
|
|
|
- mRoundCorner = (int) typedArray.getDimension(R.styleable.BannerViewPager_bvp_round_corner, 0);
|
|
|
- mRevealWidth = (int) typedArray.getDimension(R.styleable.BannerViewPager_bvp_reveal_width, 0);
|
|
|
- indicatorGravity = typedArray.getInt(R.styleable.BannerViewPager_bvp_indicator_gravity, 0);
|
|
|
- mPageStyle = typedArray.getInt(R.styleable.BannerViewPager_bvp_page_style, 0);
|
|
|
- mIndicatorStyle = typedArray.getInt(R.styleable.BannerViewPager_bvp_indicator_style, 0);
|
|
|
- mIndicatorSlideMode = typedArray.getInt(R.styleable.BannerViewPager_bvp_indicator_slide_mode, 0);
|
|
|
- mIndicatorVisibility = typedArray.getInt(R.styleable.BannerViewPager_bvp_indicator_visibility, 0);
|
|
|
- mScrollDuration = typedArray.getInt(R.styleable.BannerViewPager_bvp_scroll_duration, 800);
|
|
|
- typedArray.recycle();
|
|
|
- indicatorGap = normalIndicatorWidth;
|
|
|
- indicatorHeight = normalIndicatorWidth / 2;
|
|
|
- checkedIndicatorWidth = normalIndicatorWidth;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void initBannerData(List<T> list) {
|
|
|
- if (list != null) {
|
|
|
- mList.clear();
|
|
|
- mList.addAll(list);
|
|
|
- if (mList.size() > 0) {
|
|
|
- if (mList.size() > 1) {
|
|
|
- if (isCustomIndicator && null != mIndicatorView) {
|
|
|
- initIndicator(mIndicatorView);
|
|
|
- } else {
|
|
|
- initIndicator(IndicatorFactory.createIndicatorView(getContext(), mIndicatorStyle));
|
|
|
- }
|
|
|
- }
|
|
|
- if (isCanLoop) {
|
|
|
-// currentPosition = mPageStyle == PageStyle.NORMAL ? 1 : 2;
|
|
|
- currentPosition = MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1;
|
|
|
- }
|
|
|
- setupViewPager();
|
|
|
- setIndicatorValues();
|
|
|
- }
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ protected void onDetachedFromWindow() {
|
|
|
+ stopLoop();
|
|
|
+ super.onDetachedFromWindow();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- private void setIndicatorValues() {
|
|
|
- if (null != mIndicatorView) {
|
|
|
- mIndicatorView.setPageSize(mList.size());
|
|
|
- mIndicatorView.setCheckedColor(indicatorCheckedColor);
|
|
|
- mIndicatorView.setNormalColor(indicatorNormalColor);
|
|
|
- mIndicatorView.setIndicatorGap(indicatorGap);
|
|
|
- mIndicatorView.setSlideMode(mIndicatorSlideMode);
|
|
|
- mIndicatorView.setIndicatorWidth(normalIndicatorWidth, checkedIndicatorWidth);
|
|
|
- if (mIndicatorView instanceof DashIndicatorView) {
|
|
|
- ((DashIndicatorView) mIndicatorView).setSliderHeight(indicatorHeight);
|
|
|
- }
|
|
|
- mIndicatorView.notifyDataChanged();
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ protected void onAttachedToWindow() {
|
|
|
+ super.onAttachedToWindow();
|
|
|
+ startLoop();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 设置触摸事件,当滑动或者触摸时停止自动轮播
|
|
|
- */
|
|
|
@SuppressLint("ClickableViewAccessibility")
|
|
|
private void setTouchListener() {
|
|
|
mViewPager.setOnTouchListener((v, event) -> {
|
|
@@ -242,12 +119,12 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
switch (action) {
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
- isLooping = true;
|
|
|
+ setLooping(true);
|
|
|
stopLoop();
|
|
|
break;
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
case MotionEvent.ACTION_CANCEL:
|
|
|
- isLooping = false;
|
|
|
+ setLooping(false);
|
|
|
startLoop();
|
|
|
default:
|
|
|
break;
|
|
@@ -256,8 +133,87 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void onPageSelected(int position) {
|
|
|
+ currentPosition = BannerUtils.getRealPosition(isCanLoop(), position, mList.size());
|
|
|
+ if (mOnPageChangeListener != null)
|
|
|
+ mOnPageChangeListener.onPageSelected(currentPosition);
|
|
|
+ if (mIndicatorView != null) {
|
|
|
+ mIndicatorView.onPageSelected(currentPosition);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPageScrollStateChanged(int state) {
|
|
|
+ if (mIndicatorView != null) {
|
|
|
+ mIndicatorView.onPageScrollStateChanged(state);
|
|
|
+ }
|
|
|
+ if (mOnPageChangeListener != null) {
|
|
|
+ mOnPageChangeListener.onPageScrollStateChanged(state);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
|
|
+ if (mOnPageChangeListener != null) {
|
|
|
+ mOnPageChangeListener.onPageScrolled(BannerUtils.getRealPosition(isCanLoop(), position, mList.size()),
|
|
|
+ positionOffset, positionOffsetPixels);
|
|
|
+ }
|
|
|
+ if (mIndicatorView != null)
|
|
|
+ mIndicatorView.onPageScrolled(BannerUtils.getRealPosition(isCanLoop(), position, mList.size()),
|
|
|
+ positionOffset, positionOffsetPixels);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handlePosition() {
|
|
|
+ if (mList.size() > 1) {
|
|
|
+ currentPosition = mViewPager.getCurrentItem() + 1;
|
|
|
+ if (isCanLoop()) {
|
|
|
+ if (currentPosition == MAX_VALUE - 1) {
|
|
|
+ currentPosition = 0;
|
|
|
+ mViewPager.setCurrentItem(currentPosition, false);
|
|
|
+ mHandler.post(mRunnable);
|
|
|
+ } else {
|
|
|
+ mViewPager.setCurrentItem(currentPosition);
|
|
|
+ mHandler.postDelayed(mRunnable, getInterval());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (currentPosition >= MAX_VALUE) {
|
|
|
+ stopLoop();
|
|
|
+ } else {
|
|
|
+ mViewPager.setCurrentItem(currentPosition);
|
|
|
+ mHandler.postDelayed(mRunnable, getInterval());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initBannerData(List<T> list) {
|
|
|
+ if (list != null) {
|
|
|
+ mList.clear();
|
|
|
+ mList.addAll(list);
|
|
|
+ if (mList.size() > 0) {
|
|
|
+ if (mList.size() > 1) setIndicatorValues();
|
|
|
+ if (isCanLoop())
|
|
|
+ currentPosition = MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1;
|
|
|
+ setupViewPager();
|
|
|
+ initRoundCorner();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setIndicatorValues() {
|
|
|
+ BannerOptions bannerOptions = mBannerManager.bannerOptions();
|
|
|
+ if (bannerOptions.isCustomIndicator() && null != mIndicatorView) {
|
|
|
+ initIndicator(mIndicatorView);
|
|
|
+ } else {
|
|
|
+ initIndicator(IndicatorFactory.createIndicatorView(getContext(), bannerOptions.getIndicatorStyle()));
|
|
|
+ }
|
|
|
+ mIndicatorView.setIndicatorOptions(bannerOptions.getIndicatorOptions());
|
|
|
+ mIndicatorView.setPageSize(mList.size());
|
|
|
+ }
|
|
|
+
|
|
|
private void initIndicator(IIndicator indicatorView) {
|
|
|
- mIndicatorLayout.setVisibility(mIndicatorVisibility);
|
|
|
+ mIndicatorLayout.setVisibility(mBannerManager.bannerOptions().getIndicatorVisibility());
|
|
|
mIndicatorView = indicatorView;
|
|
|
if (((View) mIndicatorView).getParent() == null) {
|
|
|
mIndicatorLayout.removeAllViews();
|
|
@@ -270,7 +226,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
private void initIndicatorGravity() {
|
|
|
RelativeLayout.LayoutParams layoutParams =
|
|
|
(RelativeLayout.LayoutParams) ((View) mIndicatorView).getLayoutParams();
|
|
|
- switch (indicatorGravity) {
|
|
|
+ switch (mBannerManager.bannerOptions().getIndicatorGravity()) {
|
|
|
case CENTER:
|
|
|
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
|
|
|
break;
|
|
@@ -285,32 +241,34 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
|
|
|
private void initIndicatorViewMargin() {
|
|
|
ViewGroup.MarginLayoutParams layoutParams = (MarginLayoutParams) ((View) mIndicatorView).getLayoutParams();
|
|
|
- if (mIndicatorMargin == null) {
|
|
|
+ BannerOptions.IndicatorMargin indicatorMargin = mBannerManager.bannerOptions().getIndicatorMargin();
|
|
|
+ if (indicatorMargin == null) {
|
|
|
int dp10 = BannerUtils.dp2px(10);
|
|
|
layoutParams.setMargins(dp10, dp10, dp10, dp10);
|
|
|
} else {
|
|
|
- layoutParams.setMargins(mIndicatorMargin.left, mIndicatorMargin.top,
|
|
|
- mIndicatorMargin.right, mIndicatorMargin.bottom);
|
|
|
+ layoutParams.setMargins(indicatorMargin.getLeft(), indicatorMargin.getTop(),
|
|
|
+ indicatorMargin.getRight(), indicatorMargin.getBottom());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initRoundCorner() {
|
|
|
+ int roundCorner = mBannerManager.bannerOptions().getRoundCorner();
|
|
|
+ if (roundCorner > 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
+ ViewStyleSetter viewStyleSetter = new ViewStyleSetter(this);
|
|
|
+ viewStyleSetter.setRoundCorner(roundCorner);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void setupViewPager() {
|
|
|
if (holderCreator != null) {
|
|
|
removeAllViews();
|
|
|
- BannerPagerAdapter<T, VH> bannerPagerAdapter =
|
|
|
- new BannerPagerAdapter<>(mList, holderCreator);
|
|
|
- bannerPagerAdapter.setCanLoop(isCanLoop);
|
|
|
- bannerPagerAdapter.setPageClickListener(position -> {
|
|
|
- if (mOnPageClickListener != null) {
|
|
|
- mOnPageClickListener.onPageClick(position);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- mViewPager.setAdapter(bannerPagerAdapter);
|
|
|
+ mViewPager.setAdapter(getPagerAdapter());
|
|
|
mViewPager.setCurrentItem(currentPosition);
|
|
|
mViewPager.addOnPageChangeListener(this);
|
|
|
- mViewPager.setScrollDuration(mScrollDuration);
|
|
|
- mViewPager.disableTouchScroll(disableTouchScroll);
|
|
|
+ BannerOptions bannerOptions = mBannerManager.bannerOptions();
|
|
|
+ mViewPager.setScrollDuration(bannerOptions.getScrollDuration());
|
|
|
+ mViewPager.disableTouchScroll(bannerOptions.isDisableTouchScroll());
|
|
|
+ mViewPager.setFirstLayout(true);
|
|
|
addView(mViewPager);
|
|
|
addView(mIndicatorLayout);
|
|
|
initPageStyle();
|
|
@@ -319,15 +277,22 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
} else {
|
|
|
throw new NullPointerException("You must set HolderCreator for BannerViewPager");
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- if (mRoundCorner > 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
- ViewStyleSetter viewStyleSetter = new ViewStyleSetter(this);
|
|
|
- viewStyleSetter.setRoundCorner(mRoundCorner);
|
|
|
- }
|
|
|
+ private PagerAdapter getPagerAdapter() {
|
|
|
+ BannerPagerAdapter<T, VH> bannerPagerAdapter =
|
|
|
+ new BannerPagerAdapter<>(mList, holderCreator);
|
|
|
+ bannerPagerAdapter.setCanLoop(isCanLoop());
|
|
|
+ bannerPagerAdapter.setPageClickListener(position -> {
|
|
|
+ if (mOnPageClickListener != null) {
|
|
|
+ mOnPageClickListener.onPageClick(position);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return bannerPagerAdapter;
|
|
|
}
|
|
|
|
|
|
private void initPageStyle() {
|
|
|
- switch (mPageStyle) {
|
|
|
+ switch (mBannerManager.bannerOptions().getPageStyle()) {
|
|
|
case PageStyle.MULTI_PAGE:
|
|
|
setMultiPageStyle(false, MAX_SCALE);
|
|
|
break;
|
|
@@ -341,47 +306,35 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
}
|
|
|
|
|
|
private void setMultiPageStyle(boolean overlap, float scale) {
|
|
|
- mPageMargin = mPageMargin == 0 ? BannerUtils.dp2px(20) : mPageMargin;
|
|
|
- mRevealWidth = mRevealWidth == 0 ? BannerUtils.dp2px(20) : mRevealWidth;
|
|
|
setClipChildren(false);
|
|
|
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mViewPager.getLayoutParams();
|
|
|
- params.leftMargin = mPageMargin + mRevealWidth;
|
|
|
- params.rightMargin = mPageMargin + mRevealWidth;
|
|
|
+ BannerOptions bannerOptions = mBannerManager.bannerOptions();
|
|
|
+ params.leftMargin = bannerOptions.getPageMargin() + bannerOptions.getRevealWidth();
|
|
|
+ params.rightMargin = params.leftMargin;
|
|
|
mViewPager.setOverlapStyle(overlap);
|
|
|
- mViewPager.setPageMargin(overlap ? -mPageMargin : mPageMargin);
|
|
|
+ mViewPager.setPageMargin(overlap ? -bannerOptions.getPageMargin() : bannerOptions.getPageMargin());
|
|
|
mViewPager.setOffscreenPageLimit(2);
|
|
|
setPageTransformer(new ScaleInTransformer(scale));
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void onPageSelected(int position) {
|
|
|
- currentPosition = PositionUtils.getRealPosition(isCanLoop, position, mList.size());
|
|
|
- if (mOnPageChangeListener != null)
|
|
|
- mOnPageChangeListener.onPageSelected(currentPosition);
|
|
|
- if (mIndicatorView != null) {
|
|
|
- mIndicatorView.onPageSelected(currentPosition);
|
|
|
- }
|
|
|
+ private int getInterval() {
|
|
|
+ return mBannerManager.bannerOptions().getInterval();
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void onPageScrollStateChanged(int state) {
|
|
|
- if (mIndicatorView != null) {
|
|
|
- mIndicatorView.onPageScrollStateChanged(state);
|
|
|
- }
|
|
|
- if (mOnPageChangeListener != null) {
|
|
|
- mOnPageChangeListener.onPageScrollStateChanged(state);
|
|
|
- }
|
|
|
+ private boolean isAutoPlay() {
|
|
|
+ return mBannerManager.bannerOptions().isAutoPlay();
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
|
|
- if (mOnPageChangeListener != null) {
|
|
|
- mOnPageChangeListener.onPageScrolled(PositionUtils.getRealPosition(isCanLoop, position, mList.size()),
|
|
|
- positionOffset, positionOffsetPixels);
|
|
|
- }
|
|
|
- if (mIndicatorView != null)
|
|
|
- mIndicatorView.onPageScrolled(PositionUtils.getRealPosition(isCanLoop, position, mList.size()),
|
|
|
- positionOffset, positionOffsetPixels);
|
|
|
+ private boolean isLooping() {
|
|
|
+ return mBannerManager.bannerOptions().isLooping();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setLooping(boolean looping) {
|
|
|
+ mBannerManager.bannerOptions().setLooping(looping);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isCanLoop() {
|
|
|
+ return mBannerManager.bannerOptions().isCanLoop();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -395,9 +348,9 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* 开启轮播
|
|
|
*/
|
|
|
public void startLoop() {
|
|
|
- if (!isLooping && isAutoPlay && mList.size() > 1) {
|
|
|
- mHandler.postDelayed(mRunnable, interval);
|
|
|
- isLooping = true;
|
|
|
+ if (!isLooping() && isAutoPlay() && mList.size() > 1) {
|
|
|
+ mHandler.postDelayed(mRunnable, getInterval());
|
|
|
+ setLooping(true);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -405,9 +358,9 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* 停止轮播
|
|
|
*/
|
|
|
public void stopLoop() {
|
|
|
- if (isLooping) {
|
|
|
+ if (isLooping()) {
|
|
|
mHandler.removeCallbacks(mRunnable);
|
|
|
- isLooping = false;
|
|
|
+ setLooping(false);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -428,20 +381,19 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param radius 圆角大小
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setRoundCorner(int radius) {
|
|
|
- mRoundCorner = radius;
|
|
|
+ mBannerManager.bannerOptions().setRoundCorner(radius);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 设置是否自动轮播
|
|
|
*
|
|
|
* @param autoPlay 是否自动轮播
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setAutoPlay(boolean autoPlay) {
|
|
|
- isAutoPlay = autoPlay;
|
|
|
- if (isAutoPlay) {
|
|
|
- isCanLoop = true;
|
|
|
+ mBannerManager.bannerOptions().setAutoPlay(autoPlay);
|
|
|
+ if (isAutoPlay()) {
|
|
|
+ mBannerManager.bannerOptions().setCanLoop(true);
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
@@ -452,9 +404,9 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param canLoop 是否可以循环
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setCanLoop(boolean canLoop) {
|
|
|
- isCanLoop = canLoop;
|
|
|
+ mBannerManager.bannerOptions().setCanLoop(canLoop);
|
|
|
if (!canLoop) {
|
|
|
- isAutoPlay = false;
|
|
|
+ mBannerManager.bannerOptions().setAutoPlay(false);
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
@@ -465,7 +417,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param interval 自动轮播时间间隔 单位毫秒ms
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setInterval(int interval) {
|
|
|
- this.interval = interval;
|
|
|
+ mBannerManager.bannerOptions().setInterval(interval);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -501,7 +453,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param scrollDuration page滚动时间
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setScrollDuration(int scrollDuration) {
|
|
|
- mScrollDuration = scrollDuration;
|
|
|
+ mBannerManager.bannerOptions().setScrollDuration(scrollDuration);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -511,8 +463,8 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setIndicatorColor(@ColorInt int normalColor,
|
|
|
@ColorInt int checkedColor) {
|
|
|
- indicatorCheckedColor = checkedColor;
|
|
|
- indicatorNormalColor = normalColor;
|
|
|
+ mBannerManager.bannerOptions().setIndicatorCheckedColor(checkedColor);
|
|
|
+ mBannerManager.bannerOptions().setIndicatorNormalColor(normalColor);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -522,8 +474,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param radius 指示器圆点半径
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setIndicatorRadius(int radius) {
|
|
|
- this.normalIndicatorWidth = radius * 2;
|
|
|
- this.checkedIndicatorWidth = radius * 2;
|
|
|
+ setIndicatorRadius(radius,radius);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -534,8 +485,8 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param checkedRadius 选中时半径
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setIndicatorRadius(int normalRadius, int checkedRadius) {
|
|
|
- this.normalIndicatorWidth = normalRadius * 2;
|
|
|
- this.checkedIndicatorWidth = checkedRadius * 2;
|
|
|
+ mBannerManager.bannerOptions().setNormalIndicatorWidth(normalRadius * 2);
|
|
|
+ mBannerManager.bannerOptions().setCheckedIndicatorWidth(checkedRadius * 2);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -546,8 +497,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param indicatorWidth 单个Indicator宽度/直径
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setIndicatorWidth(int indicatorWidth) {
|
|
|
- this.normalIndicatorWidth = indicatorWidth;
|
|
|
- this.checkedIndicatorWidth = indicatorWidth;
|
|
|
+ setIndicatorWidth(indicatorWidth, indicatorWidth);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -559,13 +509,13 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param checkWidth 选中时宽度/直径
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setIndicatorWidth(int normalWidth, int checkWidth) {
|
|
|
- this.normalIndicatorWidth = normalWidth;
|
|
|
- this.checkedIndicatorWidth = checkWidth;
|
|
|
+ mBannerManager.bannerOptions().setNormalIndicatorWidth(normalWidth);
|
|
|
+ mBannerManager.bannerOptions().setCheckedIndicatorWidth(checkWidth);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
public BannerViewPager<T, VH> setIndicatorHeight(int indicatorHeight) {
|
|
|
- this.indicatorHeight = indicatorHeight;
|
|
|
+ mBannerManager.bannerOptions().setIndicatorHeight(indicatorHeight);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -576,7 +526,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @return BannerViewPager
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setIndicatorGap(int indicatorGap) {
|
|
|
- this.indicatorGap = indicatorGap;
|
|
|
+ mBannerManager.bannerOptions().setIndicatorGap(indicatorGap);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -591,7 +541,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
}
|
|
|
|
|
|
public BannerViewPager<T, VH> setIndicatorVisibility(@Visibility int visibility) {
|
|
|
- mIndicatorVisibility = visibility;
|
|
|
+ mBannerManager.bannerOptions().setIndicatorVisibility(visibility);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -604,7 +554,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* {@link com.zhpan.bannerview.constants.IndicatorGravity#END}
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setIndicatorGravity(@AIndicatorGravity int gravity) {
|
|
|
- this.indicatorGravity = gravity;
|
|
|
+ mBannerManager.bannerOptions().setIndicatorGravity(gravity);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -616,7 +566,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @see com.zhpan.bannerview.constants.IndicatorSlideMode#SMOOTH
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setIndicatorSlideMode(@AIndicatorSlideMode int slideMode) {
|
|
|
- mIndicatorSlideMode = slideMode;
|
|
|
+ mBannerManager.bannerOptions().setIndicatorSlideMode(slideMode);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -629,7 +579,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setIndicatorView(IIndicator customIndicator) {
|
|
|
if (customIndicator instanceof View) {
|
|
|
- isCustomIndicator = true;
|
|
|
+ mBannerManager.bannerOptions().setCustomIndicator(true);
|
|
|
mIndicatorView = customIndicator;
|
|
|
}
|
|
|
return this;
|
|
@@ -643,7 +593,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* {@link IndicatorStyle#DASH}
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setIndicatorStyle(@AIndicatorStyle int indicatorStyle) {
|
|
|
- mIndicatorStyle = indicatorStyle;
|
|
|
+ mBannerManager.bannerOptions().setIndicatorStyle(indicatorStyle);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -671,7 +621,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param item Item index to select
|
|
|
*/
|
|
|
public void setCurrentItem(int item) {
|
|
|
- mViewPager.setCurrentItem(isCanLoop ? MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1 + item : item);
|
|
|
+ mViewPager.setCurrentItem(isCanLoop() ? MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1 + item : item);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -681,7 +631,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param smoothScroll True to smoothly scroll to the new item, false to transition immediately
|
|
|
*/
|
|
|
public void setCurrentItem(int item, boolean smoothScroll) {
|
|
|
- mViewPager.setCurrentItem(isCanLoop ? MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1 + item : item, smoothScroll);
|
|
|
+ mViewPager.setCurrentItem(isCanLoop() ? MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1 + item : item, smoothScroll);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -692,7 +642,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @return BannerViewPager
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setPageStyle(@APageStyle int pageStyle) {
|
|
|
- mPageStyle = pageStyle;
|
|
|
+ mBannerManager.bannerOptions().setPageStyle(pageStyle);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -703,7 +653,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @return BannerViewPager
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setPageMargin(int pageMargin) {
|
|
|
- mPageMargin = pageMargin;
|
|
|
+ mBannerManager.bannerOptions().setPageMargin(pageMargin);
|
|
|
mViewPager.setPageMargin(pageMargin);
|
|
|
return this;
|
|
|
}
|
|
@@ -712,7 +662,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
* @param revealWidth 一屏多页模式下两边页面显露出来的宽度
|
|
|
*/
|
|
|
public BannerViewPager<T, VH> setRevealWidth(int revealWidth) {
|
|
|
- mRevealWidth = revealWidth;
|
|
|
+ mBannerManager.bannerOptions().setRevealWidth(revealWidth);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -731,29 +681,15 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
}
|
|
|
|
|
|
public BannerViewPager<T, VH> setIndicatorMargin(int left, int top, int right, int bottom) {
|
|
|
- mIndicatorMargin = new IndicatorMargin();
|
|
|
- mIndicatorMargin.bottom = bottom;
|
|
|
- mIndicatorMargin.left = left;
|
|
|
- mIndicatorMargin.top = top;
|
|
|
- mIndicatorMargin.right = right;
|
|
|
+ mBannerManager.bannerOptions().setIndicatorMargin(left, top, right, bottom);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
public BannerViewPager<T, VH> disableTouchScroll(boolean disableTouchScroll) {
|
|
|
- this.disableTouchScroll = disableTouchScroll;
|
|
|
+ mBannerManager.bannerOptions().setDisableTouchScroll(disableTouchScroll);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 仅供demo使用
|
|
|
- */
|
|
|
-// @Deprecated
|
|
|
-// public void resetIndicator() {
|
|
|
-// isCustomIndicator = false;
|
|
|
-// mIndicatorView = null;
|
|
|
-// }
|
|
|
-
|
|
|
/**
|
|
|
* 页面点击事件接口
|
|
|
*/
|
|
@@ -761,14 +697,17 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
|
|
|
void onPageClick(int position);
|
|
|
}
|
|
|
|
|
|
- private static class IndicatorMargin {
|
|
|
- private int left, right, top, bottom;
|
|
|
- }
|
|
|
-
|
|
|
private ViewPager.OnPageChangeListener mOnPageChangeListener;
|
|
|
|
|
|
public BannerViewPager<T, VH> setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
|
|
|
mOnPageChangeListener = onPageChangeListener;
|
|
|
return this;
|
|
|
}
|
|
|
+
|
|
|
+ // 仅供demo使用
|
|
|
+// @Deprecated
|
|
|
+// public void resetIndicator() {
|
|
|
+// mBannerManager.bannerOptions().setCustomIndicator(false);
|
|
|
+// mIndicatorView = null;
|
|
|
+// }
|
|
|
}
|