diff --git a/app/src/main/java/com/example/zhpan/banner/fragment/PageFragment.kt b/app/src/main/java/com/example/zhpan/banner/fragment/PageFragment.kt index 15abcf5..f6c2da1 100644 --- a/app/src/main/java/com/example/zhpan/banner/fragment/PageFragment.kt +++ b/app/src/main/java/com/example/zhpan/banner/fragment/PageFragment.kt @@ -47,7 +47,7 @@ class PageFragment : BaseFragment() { resources.getDimensionPixelOffset(dimen.dp_4), resources.getDimensionPixelOffset(dimen.dp_5) ) - setOnPageClickListener { _: View, position: Int -> itemClick(position) } + setOnPageClickListener ({ _: View, position: Int -> itemClick(position) },true) setInterval(5000) } } @@ -185,9 +185,6 @@ class PageFragment : BaseFragment() { } private fun itemClick(position: Int) { - if (position != mViewPager.currentItem) { - mViewPager.setCurrentItem(position, true) - } ToastUtils.showShort("position:$position") } diff --git a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java index 1db9a2d..87004b3 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java @@ -77,8 +77,6 @@ public class BannerViewPager extends RelativeLayout implements LifecycleObser private boolean isLooping; - private OnPageClickListener mOnPageClickListener; - private IIndicator mIndicatorView; private RelativeLayout mIndicatorLayout; @@ -403,7 +401,6 @@ private void setupViewPager(List list) { } currentPosition = 0; mBannerPagerAdapter.setCanLoop(bannerOptions.isCanLoop()); - mBannerPagerAdapter.setPageClickListener(mOnPageClickListener); mViewPager.setAdapter(mBannerPagerAdapter); if (isCanLoopSafely()) { mViewPager.setCurrentItem(getOriginalPosition(list.size()), false); @@ -674,9 +671,21 @@ public BannerViewPager setPageMargin(int pageMargin) { * @param onPageClickListener item click listener */ public BannerViewPager setOnPageClickListener(OnPageClickListener onPageClickListener) { - this.mOnPageClickListener = onPageClickListener; + setOnPageClickListener(onPageClickListener, false); + return this; + } + + public BannerViewPager setOnPageClickListener(OnPageClickListener onPageClickListener, + boolean scrollToThisItem) { if (mBannerPagerAdapter != null) { - mBannerPagerAdapter.setPageClickListener(mOnPageClickListener); + mBannerPagerAdapter.setPageClickListener( + (clickedView, realPosition, adapterPosition) -> { + onPageClickListener.onPageClick( + clickedView, realPosition); + if (scrollToThisItem) { + mViewPager.setCurrentItem(adapterPosition); + } + }); } return this; } diff --git a/bannerview/src/main/java/com/zhpan/bannerview/BaseBannerAdapter.java b/bannerview/src/main/java/com/zhpan/bannerview/BaseBannerAdapter.java index 4bffde2..b5b8f34 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BaseBannerAdapter.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BaseBannerAdapter.java @@ -36,7 +36,7 @@ public abstract class BaseBannerAdapter extends RecyclerView.Adapter mList = new ArrayList<>(); private boolean isCanLoop; public static final int MAX_VALUE = 1000; - private BannerViewPager.OnPageClickListener mPageClickListener; + private PageClickListener mPageClickListener; @NonNull @Override @@ -48,8 +48,8 @@ public final BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int int adapterPosition = viewHolder.getAdapterPosition(); if (mPageClickListener != null && adapterPosition != RecyclerView.NO_POSITION) { int realPosition = - BannerUtils.getRealPosition(viewHolder.getAdapterPosition(), getListSize()); - mPageClickListener.onPageClick(clickedView, realPosition); + BannerUtils.getRealPosition(adapterPosition, getListSize()); + mPageClickListener.onPageClick(clickedView, realPosition, adapterPosition); } }); return viewHolder; @@ -91,7 +91,7 @@ void setCanLoop(boolean canLoop) { isCanLoop = canLoop; } - void setPageClickListener(BannerViewPager.OnPageClickListener pageClickListener) { + void setPageClickListener(PageClickListener pageClickListener) { mPageClickListener = pageClickListener; } @@ -139,4 +139,8 @@ public BaseViewHolder createViewHolder(@NonNull ViewGroup parent, View itemVi */ public abstract @LayoutRes int getLayoutId(int viewType); + + interface PageClickListener { + void onPageClick(View clickedView, int realPosition, int adapterPosition); + } }