diff --git a/README.md b/README.md index 45917c37..d64d4b52 100644 --- a/README.md +++ b/README.md @@ -61,17 +61,17 @@ BannerViewPager支持多种IndicatorViewStyle,同时还提供了完全自定义I | BannerViewPager setIndicatorStyle(int indicatorStyle) | 设置指示器样式 | 可选枚举(CIRCLE, DASH) 默认CIRCLE | | BannerViewPager setIndicatorGravity(int gravity) | 指示器位置 |可选值(CENTER、START、END)默认值CENTER | | BannerViewPager setIndicatorColor(int normalColor,int checkedColor) | 指示器圆点颜色 |normalColor:未选中时颜色默认"#8C6C6D72", checkedColor:选中时颜色 默认"#8C18171C" | -| BannerViewPager setIndicatorSlideMode(int slideMode) | 设置Indicator滑动模式 | 可选(NORMAL、SMOOTH),默认值SMOOTH | +| BannerViewPager setIndicatorSlideMode(int slideMode) | 设置Indicator滑动模式 | 可选(NORMAL、SMOOTH),默认值NORMAL | | BannerViewPager setIndicatorRadius(int radius) | 设置指示器圆点半径 | 默认值4dp| | BannerViewPager setIndicatorRadius(int normalRadius,int checkRadius) |设置指示器圆点半径 | normalRadius:未选中时半径 checkedRadius:选中时的半径,默认值4dp | | BannerViewPager setIndicatorWidth(int indicatorWidth) | 设置指示器宽度,如果是圆形指示器,则为直径 | 默认值8dp| | BannerViewPager setIndicatorWidth(int normalWidth, int checkWidth) | 设置指示器宽度,如果是圆形指示器,则为直径 | 默认值8dp | | BannerViewPager setIndicatorHeight(int indicatorHeight) | 设置指示器高度,仅在Indicator样式为DASH时有效 | 默认值normalIndicatorWidth/2 | | BannerViewPager setIndicatorGap(int indicatorMargin) | 指示器圆点间距| 默认值为指示器宽度(或者是圆的直径)| -| BannerViewPager setIndicatorView(IIndicator indicatorView) | 设置自定义指示器| | +| BannerViewPager setIndicatorView(IIndicator indicatorView) | 设置自定义指示器|自定义View需要继承BaseIndicatorView或实现IIndicator | | BannerViewPager setPageTransformerStyle(int style) | 设置页面Transformer内置样式 | | | BannerViewPager setCurrentItem(int item) | Set the currently selected page. | 2.3.5新增 | -| void getCurrentItem() | 获取当前position | 2.3.5新增 | +| int getCurrentItem() | 获取当前position | 2.3.5新增 | | BannerViewPager setPageStyle(PageStyle pageStyle) | 设置页面样式 | 2.4.0新增 可选(MULTI_PAGE、NORMAL)MULTI_PAGE:一屏多页样式 | | BannerViewPager setPageMargin(int pageMargin) | 设置页面间隔 | 2.4.0新增 | | BannerViewPager setIndicatorMargin(int left, int top, int right, int bottom) | 设置Indicator边距 | 2.4.1新增 | @@ -84,7 +84,7 @@ BannerViewPager支持多种IndicatorViewStyle,同时还提供了完全自定义I ### xml支持的attrs | Attributes | format | description | |--|--|--| -| bvp_interval | integer | 自动轮播事件间隔 | +| bvp_interval | integer | 自动轮播时间间隔 | | bvp_scroll_duration | integer | 页面切换时滑动时间| | bvp_can_loop | boolean| 是否循环 | | bvp_auto_play | boolean | 是否自动播放 | @@ -258,6 +258,8 @@ public class NetViewHolder implements ViewHolder { ``` ### 7.高级功能---自定义IndicatorView +在内置Indicator不满足需求时可以通过自定义IndicatorView实现。 + **(1)自定义View并继承BaseIndicatorView** ``` @@ -322,6 +324,19 @@ public class FigureIndicatorView extends BaseIndicatorView { } } ``` +** (2)设置自定义指示器 ** + +``` + FigureIndicatorView indicatorView = new FigureIndicatorView(mContext); + indicatorView.setRadius(BannerUtils.dp2px(18)); + indicatorView.setTextSize(BannerUtils.dp2px(13)); + indicatorView.setBackgroundColor(Color.parseColor("#aa118EEA")); + + mViewPager.setIndicatorGravity(IndicatorGravity.END) + .setIndicatorView(indicatorView) + .setHolderCreator(() -> new ImageResourceViewHolder(0)) + .create(mDrawableList); +``` ## TODO 版本计划 diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/activity/WelcomeActivity.java b/app/src/main/java/com/example/zhpan/circleviewpager/activity/WelcomeActivity.java index 14c6f42f..a06dfb01 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/activity/WelcomeActivity.java +++ b/app/src/main/java/com/example/zhpan/circleviewpager/activity/WelcomeActivity.java @@ -1,11 +1,11 @@ package com.example.zhpan.circleviewpager.activity; -import android.animation.Animator; -import android.animation.AnimatorListenerAdapter; +import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.os.Bundle; import android.view.View; +import android.view.animation.DecelerateInterpolator; import android.widget.TextView; import android.widget.Toast; @@ -17,6 +17,7 @@ import com.zhpan.bannerview.constants.IndicatorSlideMode; import com.zhpan.bannerview.constants.TransformerStyle; import com.zhpan.bannerview.holder.HolderCreator; +import com.zhpan.bannerview.utils.BannerUtils; import java.util.ArrayList; import java.util.List; @@ -38,12 +39,18 @@ public class WelcomeActivity extends BaseDataActivity implements @BindView(R.id.btn_start) TextView mTvStart; + @BindView(R.id.tv_describe) + TextView mTvDescription; + + private static final int ANIMATION_DURATION = 1300; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); ButterKnife.bind(this); setupViewPager(); + updateUI(0); } @Override @@ -57,8 +64,8 @@ private void setupViewPager() { mViewPager.setAutoPlay(false) .setCanLoop(false) .setPageTransformerStyle(transforms[new Random().nextInt(5)]) - .setIndicatorVisibility(View.GONE) - .setIndicatorView(findViewById(R.id.indicator)) + .setScrollDuration(ANIMATION_DURATION) + .setIndicatorMargin(0, 0, 0, BannerUtils.dp2px(100)) .setIndicatorGap((int) getResources().getDimension(R.dimen.dp_10)) .setIndicatorColor(getResources().getColor(R.color.white), getResources().getColor(R.color.white_alpha_75)) @@ -67,7 +74,7 @@ private void setupViewPager() { .setOnPageChangeListener(new OnPageChangeListenerAdapter() { @Override public void onPageSelected(int position) { - showStartButton(position); + updateUI(position); } }) .setHolderCreator(this) @@ -80,12 +87,22 @@ public void onClick(View view) { finish(); } - private void showStartButton(int position) { + private void updateUI(int position) { + mTvDescription.setText(des[position]); + ObjectAnimator translationAnim = ObjectAnimator.ofFloat(mTvDescription, "translationX", -120, 0); + translationAnim.setDuration(ANIMATION_DURATION); + translationAnim.setInterpolator(new DecelerateInterpolator()); + ObjectAnimator alphaAnimator1 = ObjectAnimator.ofFloat(mTvDescription, "alpha", 0, 1); + alphaAnimator1.setDuration(ANIMATION_DURATION); + AnimatorSet animatorSet = new AnimatorSet(); + animatorSet.playTogether(translationAnim, alphaAnimator1); + animatorSet.start(); + if (position == mViewPager.getList().size() - 1 && mTvStart.getVisibility() == View.GONE) { mTvStart.setVisibility(View.VISIBLE); ObjectAnimator .ofFloat(mTvStart, "alpha", 0, 1) - .setDuration(1500).start(); + .setDuration(ANIMATION_DURATION).start(); } else { mTvStart.setVisibility(View.GONE); } 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 71339756..9bbe57c1 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 @@ -58,11 +58,11 @@ private void initRadioGroup() { radioGroupStyle.setOnCheckedChangeListener((group, checkedId) -> { switch (checkedId) { case R.id.rb_circle: - mViewPager.resetIndicator(); +// mViewPager.resetIndicator(); setupCircleIndicator(); break; case R.id.rb_dash: - mViewPager.resetIndicator(); +// mViewPager.resetIndicator(); setupDashIndicator(); break; case R.id.rb_custom: @@ -78,7 +78,6 @@ private void setupCircleIndicator() { .setIndicatorGravity(IndicatorGravity.CENTER) .setIndicatorGap(BannerUtils.dp2px(6)) .setPageMargin(0) - .setIndicatorMargin(0, 0, 0, BannerUtils.dp2px(10)) .setOnPageClickListener(position -> ToastUtils.show("position:" + position)) .setIndicatorColor(getColor(R.color.red_normal_color), getColor(R.color.red_checked_color)) .setIndicatorRadius(BannerUtils.dp2px(4), BannerUtils.dp2px(5)).create(mDrawableList); @@ -98,7 +97,6 @@ private void setupDashIndicator() { private void setupCustomIndicator() { mViewPager.setAutoPlay(false).setCanLoop(true) .setPageMargin(BannerUtils.dp2px(20)) - .setIndicatorMargin(0, 0, BannerUtils.dp2px(10), BannerUtils.dp2px(10)) .setIndicatorGravity(IndicatorGravity.END) .setIndicatorView(setupIndicatorView()) .setHolderCreator(() -> new ImageResourceViewHolder(0)).create(mDrawableList); 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 f0e01c27..19718949 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 @@ -70,6 +70,7 @@ public static PageFragment getInstance() { private void setupBanner(@APageStyle int pageStyle) { mViewPager + .setIndicatorVisibility(View.VISIBLE) // 在实际开发中这行代码不必添加,此处因为受到其它两种模式影响所以要隐藏掉内置指示器 .setPageStyle(pageStyle) .create(mDrawableList); } @@ -78,12 +79,18 @@ private void initRadioGroup() { mRadioGroupPageStyle.setOnCheckedChangeListener((group, checkedId) -> { switch (checkedId) { case R.id.rb_multi_page: +// mViewPager.resetIndicator();// 在实际开发中这行代码不必添加,此处因为受到其它两种模式影响所以要隐藏掉内置指示器 + indicatorView.setVisibility(View.INVISIBLE);// 在实际开发中这行代码不必添加,此处因为受到其它两种模式影响所以要隐藏掉内置指示器 setupBanner(PageStyle.MULTI_PAGE); break; case R.id.rb_multi_page_scale: +// mViewPager.resetIndicator();// 在实际开发中这行代码不必添加,此处因为受到其它两种模式影响所以要隐藏掉内置指示器 + indicatorView.setVisibility(View.INVISIBLE);// 在实际开发中这行代码不必添加,此处因为受到其它两种模式影响所以要隐藏掉内置指示器 setupBanner(PageStyle.MULTI_PAGE_SCALE); break; case R.id.rb_multi_page_overlap: + indicatorView.setVisibility(View.VISIBLE);// 在实际开发中这行代码不必添加,此处因为受到其它两种模式影响所以要隐藏掉内置指示器 +// mViewPager.resetIndicator(); setupOverlapBanner(); break; } @@ -96,8 +103,6 @@ private void setupOverlapBanner() { .setIndicatorVisibility(View.GONE) // 在实际开发中这行代码不必添加,此处因为受到其它两种模式影响所以要隐藏掉内置指示器 .setPageStyle(PageStyle.MULTI_PAGE_OVERLAP) .setIndicatorView(indicatorView) - .setIndicatorColor(Color.parseColor("#888888"), - Color.parseColor("#118EEA")) .create(mDrawableList); } diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/viewholder/CustomPageViewHolder.java b/app/src/main/java/com/example/zhpan/circleviewpager/viewholder/CustomPageViewHolder.java index 2f365ee6..8464d256 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/viewholder/CustomPageViewHolder.java +++ b/app/src/main/java/com/example/zhpan/circleviewpager/viewholder/CustomPageViewHolder.java @@ -1,14 +1,11 @@ package com.example.zhpan.circleviewpager.viewholder; -import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.animation.DecelerateInterpolator; import android.widget.ImageView; -import android.widget.TextView; import com.example.zhpan.circleviewpager.R; import com.example.zhpan.circleviewpager.bean.CustomBean; @@ -16,7 +13,6 @@ public class CustomPageViewHolder implements ViewHolder { private ImageView mImageView; - private TextView mTextView; private ImageView mImageStart; private OnSubViewClickListener mOnSubViewClickListener; @@ -24,7 +20,6 @@ public class CustomPageViewHolder implements ViewHolder { public View createView(ViewGroup viewGroup, Context context, int position) { View view = LayoutInflater.from(context).inflate(R.layout.item_custom_view, viewGroup, false); mImageView = view.findViewById(R.id.banner_image); - mTextView = view.findViewById(R.id.tv_describe); mImageStart = view.findViewById(R.id.iv_logo); return view; } @@ -32,24 +27,13 @@ public View createView(ViewGroup viewGroup, Context context, int position) { @Override public void onBind(Context context, CustomBean data, int position, int size) { mImageView.setImageResource(data.getImageRes()); - mTextView.setText(data.getImageDescription()); mImageStart.setOnClickListener(view -> { if (null != mOnSubViewClickListener) mOnSubViewClickListener.onViewClick(view, position); }); ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mImageStart, "alpha", 0, 1); alphaAnimator.setDuration(1500); - - ObjectAnimator translationAnim = ObjectAnimator.ofFloat(mTextView, "translationX", -120, 0); - translationAnim.setDuration(1500); - translationAnim.setInterpolator(new DecelerateInterpolator()); - - ObjectAnimator alphaAnimator1 = ObjectAnimator.ofFloat(mTextView, "alpha", 0, 1); - alphaAnimator1.setDuration(1500); - - AnimatorSet animatorSet = new AnimatorSet(); - animatorSet.playTogether(alphaAnimator, translationAnim, alphaAnimator1); - animatorSet.start(); + alphaAnimator.start(); } diff --git a/app/src/main/res/layout/activity_welcome.xml b/app/src/main/res/layout/activity_welcome.xml index 50567b44..4414e07e 100644 --- a/app/src/main/res/layout/activity_welcome.xml +++ b/app/src/main/res/layout/activity_welcome.xml @@ -9,13 +9,16 @@ android:layout_width="match_parent" android:layout_height="match_parent" /> - + android:layout_marginStart="50dp" + android:layout_marginBottom="150dp" + android:lineSpacingExtra="5dp" + android:textColor="#FFFFFF" + android:textSize="18sp" /> - \ No newline at end of file diff --git a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java index 9961fa58..be5cb57e 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java @@ -42,6 +42,7 @@ import java.util.ArrayList; import java.util.List; +import static com.zhpan.bannerview.adapter.BannerPagerAdapter.MAX_VALUE; import static com.zhpan.bannerview.constants.IndicatorGravity.CENTER; import static com.zhpan.bannerview.constants.IndicatorGravity.END; import static com.zhpan.bannerview.constants.IndicatorGravity.START; @@ -110,13 +111,23 @@ public class BannerViewPager extends RelativeLayout im @Override public void run() { if (mList.size() > 1) { - currentPosition = currentPosition % (mList.size() + 1) + 1; - if (currentPosition == 1) { - mViewPager.setCurrentItem(currentPosition, false); - mHandler.post(mRunnable); + 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 { - mViewPager.setCurrentItem(currentPosition, true); - mHandler.postDelayed(mRunnable, interval); + if (currentPosition >= MAX_VALUE) { + stopLoop(); + } else { + mViewPager.setCurrentItem(currentPosition); + mHandler.postDelayed(mRunnable, interval); + } } } } @@ -196,7 +207,8 @@ private void initBannerData(List list) { } } if (isCanLoop) { - currentPosition = mPageStyle == PageStyle.NORMAL ? 1 : 2; +// currentPosition = mPageStyle == PageStyle.NORMAL ? 1 : 2; + currentPosition = MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1; } setupViewPager(); setIndicatorValues(); @@ -284,20 +296,24 @@ private void initIndicatorViewMargin() { private void setupViewPager() { if (holderCreator != null) { + removeAllViews(); BannerPagerAdapter bannerPagerAdapter = new BannerPagerAdapter<>(mList, holderCreator); bannerPagerAdapter.setPageStyle(mPageStyle); + bannerPagerAdapter.setCanLoop(isCanLoop); bannerPagerAdapter.setPageClickListener(position -> { if (mOnPageClickListener != null) { mOnPageClickListener.onPageClick(PositionUtils.getRealPosition(isCanLoop, position, mList.size(), mPageStyle)); } }); - bannerPagerAdapter.setCanLoop(isCanLoop); + mViewPager.setAdapter(bannerPagerAdapter); mViewPager.setCurrentItem(currentPosition); mViewPager.addOnPageChangeListener(this); mViewPager.setScrollDuration(mScrollDuration); mViewPager.disableTouchScroll(disableTouchScroll); + addView(mViewPager); + addView(mIndicatorLayout); initPageStyle(); startLoop(); setTouchListener(); @@ -341,14 +357,11 @@ private void setMultiPageStyle(boolean overlap, float scale) { @Override public void onPageSelected(int position) { if (mOnPageChangeListener != null) - mOnPageChangeListener.onPageSelected(PositionUtils.getRealPosition(isCanLoop, position, mList.size(), mPageStyle)); - + mOnPageChangeListener.onPageSelected(PositionUtils.getRealPosition(isCanLoop, position, mList.size())); if (mIndicatorView != null) { - mIndicatorView.onPageSelected(PositionUtils.getRealPosition(isCanLoop, position, mList.size(), mPageStyle)); + mIndicatorView.onPageSelected(PositionUtils.getRealPosition(isCanLoop, position, mList.size())); } - currentPosition = position; - } @Override @@ -359,33 +372,34 @@ public void onPageScrollStateChanged(int state) { if (mOnPageChangeListener != null) { mOnPageChangeListener.onPageScrollStateChanged(state); } - if (isCanLoop) { - switch (state) { - case ViewPager.SCROLL_STATE_IDLE: - if (currentPosition == 0) { - mViewPager.setCurrentItem(mList.size(), false); - } else if (currentPosition == mList.size() + 1) { - mViewPager.setCurrentItem(1, false); - } - break; - case ViewPager.SCROLL_STATE_DRAGGING: - if (currentPosition == mList.size() + 1) { - mViewPager.setCurrentItem(1, false); - } else if (currentPosition == 0) { - mViewPager.setCurrentItem(mList.size(), false); - } - break; - } - } +// if (isCanLoop) { +// switch (state) { +// case ViewPager.SCROLL_STATE_IDLE: +// if (currentPosition == 0) { +// mViewPager.setCurrentItem(mList.size(), false); +// } else if (currentPosition == mList.size() + 1) { +// mViewPager.setCurrentItem(1, false); +// } +// break; +// case ViewPager.SCROLL_STATE_DRAGGING: +// if (currentPosition == mList.size() + 1) { +// mViewPager.setCurrentItem(1, false); +// } else if (currentPosition == 0) { +// mViewPager.setCurrentItem(mList.size(), false); +// } +// break; +// } +// } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (mOnPageChangeListener != null) { - mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels); + mOnPageChangeListener.onPageScrolled(PositionUtils.getRealPosition(isCanLoop, position, mList.size()), + positionOffset, positionOffsetPixels); } if (mIndicatorView != null) - mIndicatorView.onPageScrolled(PositionUtils.getRealPosition(isCanLoop, position, mList.size(), mPageStyle), + mIndicatorView.onPageScrolled(PositionUtils.getRealPosition(isCanLoop, position, mList.size()), positionOffset, positionOffsetPixels); } @@ -428,7 +442,7 @@ public BannerViewPager setHolderCreator(HolderCreator holderCreator) } /** - * 设置圆角ViewPager + * 设置圆角ViewPager 只有在SDK_INT>=LOLLIPOP(API 21)时有效 * * @param radius 圆角大小 */ @@ -467,7 +481,7 @@ public BannerViewPager setCanLoop(boolean canLoop) { /** * 设置自动轮播时间间隔 * - * @param interval 自动轮播时间间隔 + * @param interval 自动轮播时间间隔 单位毫秒ms */ public BannerViewPager setInterval(int interval) { this.interval = interval; @@ -525,7 +539,6 @@ public BannerViewPager setIndicatorColor(@ColorInt int normalColor, * 设置指示器半径大小,选中与未选中半径大小相等 * * @param radius 指示器圆点半径 - * @return */ public BannerViewPager setIndicatorRadius(int radius) { this.normalIndicatorWidth = radius * 2; @@ -536,12 +549,12 @@ public BannerViewPager setIndicatorRadius(int radius) { /** * 设置Indicator半径 * - * @param normalRadius 未选中时半径 - * @param checkRadius 选中时半径 + * @param normalRadius 未选中时半径 + * @param checkedRadius 选中时半径 */ - public BannerViewPager setIndicatorRadius(int normalRadius, int checkRadius) { + public BannerViewPager setIndicatorRadius(int normalRadius, int checkedRadius) { this.normalIndicatorWidth = normalRadius * 2; - this.checkedIndicatorWidth = checkRadius * 2; + this.checkedIndicatorWidth = checkedRadius * 2; return this; } @@ -754,11 +767,11 @@ public BannerViewPager disableTouchScroll(boolean disableTouchScroll) { /** * 仅供demo使用 */ - @Deprecated - public void resetIndicator() { - isCustomIndicator = false; - mIndicatorView = null; - } +// @Deprecated +// public void resetIndicator() { +// isCustomIndicator = false; +// mIndicatorView = null; +// } /** * 页面点击事件接口 diff --git a/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java b/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java index fe1ab645..0566fe48 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java @@ -7,10 +7,8 @@ import android.view.ViewGroup; import com.zhpan.bannerview.annotation.APageStyle; -import com.zhpan.bannerview.constants.PageStyle; import com.zhpan.bannerview.holder.HolderCreator; import com.zhpan.bannerview.holder.ViewHolder; -import com.zhpan.bannerview.utils.BannerUtils; import com.zhpan.bannerview.utils.PositionUtils; import java.util.ArrayList; @@ -34,6 +32,8 @@ public class BannerPagerAdapter extends PagerAdapter { private int mPageStyle; + public static final int MAX_VALUE = Integer.MAX_VALUE; + public BannerPagerAdapter(List list, HolderCreator holderCreator) { this.mList = list; this.holderCreator = holderCreator; @@ -42,11 +42,7 @@ public BannerPagerAdapter(List list, HolderCreator holderCreator) { @Override public int getCount() { if (isCanLoop && mList.size() > 1) { - if (mPageStyle == PageStyle.NORMAL) { - return mList.size() + 2; - } else { - return mList.size() + 4; - } + return MAX_VALUE; } else { return mList.size(); } @@ -60,7 +56,7 @@ public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { @Override public @NonNull Object instantiateItem(@NonNull final ViewGroup container, final int position) { - View itemView = findViewByPosition(container, PositionUtils.getRealPosition(isCanLoop, position, mList.size(), mPageStyle)); + View itemView = findViewByPosition(container, PositionUtils.getRealPosition(isCanLoop, position, mList.size())); container.addView(itemView); return itemView; } diff --git a/bannerview/src/main/java/com/zhpan/bannerview/indicator/BaseIndicatorView.java b/bannerview/src/main/java/com/zhpan/bannerview/indicator/BaseIndicatorView.java index 1d6dd09b..099d87bd 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/indicator/BaseIndicatorView.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/indicator/BaseIndicatorView.java @@ -93,11 +93,9 @@ public void onPageSelected(int position) { invalidate(); } else if (slideMode == IndicatorSlideMode.SMOOTH) { if (position == 0 && slideToRight) { -// Log.e(tag, "slideToRight position-----》" + position); currentPosition = 0; slideProgress = 0; invalidate(); - } else if (position == pageSize - 1 && !slideToRight) { currentPosition = pageSize - 1; slideProgress = 0; @@ -106,25 +104,15 @@ public void onPageSelected(int position) { } } - private static final String tag = "BaseIndicatorView"; - @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (slideMode == IndicatorSlideMode.SMOOTH) { - if ((prePosition == 0 && position == pageSize - 1)) { - slideToRight = false; - } else if (prePosition == pageSize - 1 && position == 0) { -// Log.e(tag, "prePosition-----》" + prePosition); -// Log.e(tag, "position-----》" + position); - slideToRight = true; - } else { - slideToRight = (position + positionOffset - prePosition) > 0; - } + slideToRight = isSlideToRight(position, positionOffset); // TODO 解决滑动过快时positionOffset不会等0的情况 if (positionOffset == 0) { prePosition = position; } - if (!(position == pageSize - 1 && slideToRight || (position == pageSize - 1 && !slideToRight))) { + if (!(position == pageSize - 1)) { slideProgress = (currentPosition == pageSize - 1) && slideToRight ? 0 : positionOffset; currentPosition = position; invalidate(); @@ -132,6 +120,16 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse } } + private boolean isSlideToRight(int position, float positionOffset) { + if ((prePosition == 0 && position == pageSize - 1)) { + return false; + } else if (prePosition == pageSize - 1 && position == 0) { + return true; + } else { + return (position + positionOffset - prePosition) > 0; + } + } + @Override public void setPageSize(int pageSize) { this.pageSize = pageSize; diff --git a/bannerview/src/main/java/com/zhpan/bannerview/indicator/CircleIndicatorView.java b/bannerview/src/main/java/com/zhpan/bannerview/indicator/CircleIndicatorView.java index d35bb0bd..96543714 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/indicator/CircleIndicatorView.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/indicator/CircleIndicatorView.java @@ -8,7 +8,6 @@ * Created by zhpan on 2017/12/6. */ public class CircleIndicatorView extends BaseIndicatorView { - private static final String tag = "IndicatorView"; private float mNormalRadius; private float mCheckedRadius; private float maxRadius; diff --git a/bannerview/src/main/java/com/zhpan/bannerview/utils/PositionUtils.java b/bannerview/src/main/java/com/zhpan/bannerview/utils/PositionUtils.java index bd78f477..70f3a301 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/utils/PositionUtils.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/utils/PositionUtils.java @@ -16,6 +16,20 @@ public static int toUnrealPosition(boolean isCanLoop, int position, int pageSize } } + public static int getRealPosition(boolean isCanLoop, int position, int pageSize) { + int realPosition; + if (pageSize <= 0) + return 0; + if (isCanLoop) { + realPosition = (position - 1 + pageSize) % pageSize; + } else { + realPosition = (position + pageSize) % pageSize; + } + if (realPosition < 0) + realPosition += pageSize; + return realPosition; + } + public static int getRealPosition(boolean isCanLoop, int position, int pageSize, int pageStyle) { if (isCanLoop) { if (pageStyle == PageStyle.NORMAL) { diff --git a/download/app.apk b/download/app.apk index 3c1cc1ab..164410f0 100644 Binary files a/download/app.apk and b/download/app.apk differ diff --git a/image/transform_accordion.gif b/image/transform_accordion.gif index 458cf4fb..5459e9d8 100644 Binary files a/image/transform_accordion.gif and b/image/transform_accordion.gif differ diff --git a/image/transform_depth.gif b/image/transform_depth.gif index 0164d9c4..ea2d2f7a 100644 Binary files a/image/transform_depth.gif and b/image/transform_depth.gif differ diff --git a/image/transform_rotate.gif b/image/transform_rotate.gif index b985b86b..1f7a8bf4 100644 Binary files a/image/transform_rotate.gif and b/image/transform_rotate.gif differ diff --git a/image/transform_stack.gif b/image/transform_stack.gif index 01202443..93d5cdf7 100644 Binary files a/image/transform_stack.gif and b/image/transform_stack.gif differ