Skip to content

Commit

Permalink
New function,Custom IndicatorView for BannerViewPager
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangpan-alo7 committed Sep 2, 2019
1 parent 41655c8 commit 0c674bc
Showing 1 changed file with 37 additions and 24 deletions.
61 changes: 37 additions & 24 deletions bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.zhpan.bannerview.indicator.IIndicator;
import com.zhpan.bannerview.utils.DpUtils;
import com.zhpan.bannerview.adapter.BannerPagerAdapter;
import com.zhpan.bannerview.enums.IndicatorSlideMode;
Expand Down Expand Up @@ -76,7 +77,7 @@ public class BannerViewPager<T, VH extends ViewHolder> extends FrameLayout imple
// 页面点击事件监听
private OnPageClickListener mOnPageClickListener;
// 圆点指示器的Layout
private IndicatorView mIndicatorView;
private IIndicator mIndicatorView;

private IndicatorSlideMode mIndicatorSlideMode = IndicatorSlideMode.SMOOTH;

Expand Down Expand Up @@ -171,14 +172,24 @@ private void initScroller() {
*/
private void initData() {
if (mList.size() > 0) {
initIndicator();
if (mList.size() > 1 && showIndicator) {
initIndicator(getIndicatorView());
}
if (isCanLoop) {
currentPosition = 1;
}
setupViewPager();
}
}

private View getIndicatorView() {
IndicatorView indicatorView = new IndicatorView(getContext());
indicatorView.setPageSize(mList.size()).setIndicatorRadius(normalIndicatorRadius, checkedIndicatorRadius)
.setIndicatorMargin(indicatorMargin).setCheckedColor(indicatorCheckedColor)
.setNormalColor(indicatorNormalColor).setSlideStyle(mIndicatorSlideMode).invalidate();
return indicatorView;
}


/**
* 设置触摸事件,当滑动或者触摸时停止自动轮播
Expand Down Expand Up @@ -207,28 +218,23 @@ private void setTouchListener() {
/**
* 构造指示器
*/
private void initIndicator() {
if (mList.size() > 1 && showIndicator) {
mIndicatorView = new IndicatorView(getContext());
mRelativeLayout.removeAllViews();
mRelativeLayout.addView(mIndicatorView);
mIndicatorView.setPageSize(mList.size()).setIndicatorRadius(normalIndicatorRadius, checkedIndicatorRadius)
.setIndicatorMargin(indicatorMargin).setCheckedColor(indicatorCheckedColor)
.setNormalColor(indicatorNormalColor).setSlideStyle(mIndicatorSlideMode).invalidate();
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) mIndicatorView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
switch (gravity) {
case CENTER:
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
break;
case START:
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
break;
case END:
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
break;
}
private void initIndicator(View indicatorView) {
mRelativeLayout.removeAllViews();
mRelativeLayout.addView(indicatorView);
mIndicatorView = (IIndicator) indicatorView;
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) indicatorView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
switch (gravity) {
case CENTER:
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
break;
case START:
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
break;
case END:
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
break;
}
}

Expand Down Expand Up @@ -438,6 +444,13 @@ public BannerViewPager<T, VH> setIndicatorRadius(float normalRadius, float check
return this;
}

public BannerViewPager<T, VH> setIndicatorView(IIndicator indicatorView) {
if (indicatorView instanceof View) {
initIndicator((View) indicatorView);
}
return this;
}

/**
* 设置指示器半径大小
*
Expand Down

0 comments on commit 0c674bc

Please sign in to comment.