Skip to content

Commit

Permalink
Try to Fix #298 and #252
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip committed Feb 4, 2023
1 parent c981d90 commit 100c49f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -185,9 +185,6 @@ class PageFragment : BaseFragment() {
}

private fun itemClick(position: Int) {
if (position != mViewPager.currentItem) {
mViewPager.setCurrentItem(position, true)
}
ToastUtils.showShort("position:$position")
}

Expand Down
19 changes: 14 additions & 5 deletions bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser

private boolean isLooping;

private OnPageClickListener mOnPageClickListener;

private IIndicator mIndicatorView;

private RelativeLayout mIndicatorLayout;
Expand Down Expand Up @@ -403,7 +401,6 @@ private void setupViewPager(List<T> list) {
}
currentPosition = 0;
mBannerPagerAdapter.setCanLoop(bannerOptions.isCanLoop());
mBannerPagerAdapter.setPageClickListener(mOnPageClickListener);
mViewPager.setAdapter(mBannerPagerAdapter);
if (isCanLoopSafely()) {
mViewPager.setCurrentItem(getOriginalPosition(list.size()), false);
Expand Down Expand Up @@ -674,9 +671,21 @@ public BannerViewPager<T> setPageMargin(int pageMargin) {
* @param onPageClickListener item click listener
*/
public BannerViewPager<T> setOnPageClickListener(OnPageClickListener onPageClickListener) {
this.mOnPageClickListener = onPageClickListener;
setOnPageClickListener(onPageClickListener, false);
return this;
}

public BannerViewPager<T> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class BaseBannerAdapter<T> extends RecyclerView.Adapter<BaseView
protected List<T> mList = new ArrayList<>();
private boolean isCanLoop;
public static final int MAX_VALUE = 1000;
private BannerViewPager.OnPageClickListener mPageClickListener;
private PageClickListener mPageClickListener;

@NonNull
@Override
Expand All @@ -48,8 +48,8 @@ public final BaseViewHolder<T> 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;
Expand Down Expand Up @@ -91,7 +91,7 @@ void setCanLoop(boolean canLoop) {
isCanLoop = canLoop;
}

void setPageClickListener(BannerViewPager.OnPageClickListener pageClickListener) {
void setPageClickListener(PageClickListener pageClickListener) {
mPageClickListener = pageClickListener;
}

Expand Down Expand Up @@ -139,4 +139,8 @@ public BaseViewHolder<T> createViewHolder(@NonNull ViewGroup parent, View itemVi
*/
public abstract @LayoutRes
int getLayoutId(int viewType);

interface PageClickListener {
void onPageClick(View clickedView, int realPosition, int adapterPosition);
}
}

0 comments on commit 100c49f

Please sign in to comment.