From d13fa7103a7eb0d304a24ccd1097ec062c9b21d5 Mon Sep 17 00:00:00 2001 From: zhpanvip Date: Fri, 20 Dec 2019 12:25:32 +0800 Subject: [PATCH] Optimize BannerViewPager --- .../com/zhpan/bannerview/BannerViewPager.java | 76 +++++-------------- 1 file changed, 18 insertions(+), 58 deletions(-) diff --git a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java index 0a590ae6..51da7ba5 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java @@ -1,6 +1,5 @@ package com.zhpan.bannerview; -import android.annotation.SuppressLint; import android.content.Context; import android.os.Build; import android.os.Handler; @@ -115,7 +114,7 @@ protected void onAttachedToWindow() { startLoop(); } - //触碰控件的时候,翻页应该停止,离开的时候如果之前是开启了翻页的话则重新启动翻页 + // 触碰控件的时候,翻页应该停止,离开的时候如果之前是开启了翻页的话则重新启动翻页 @Override public boolean dispatchTouchEvent(MotionEvent ev) { int action = ev.getAction(); @@ -131,30 +130,6 @@ public boolean dispatchTouchEvent(MotionEvent ev) { return super.dispatchTouchEvent(ev); } - @SuppressLint("ClickableViewAccessibility") - private void setTouchListener() { - mViewPager.setOnTouchListener(new OnTouchListener() { - @Override - public boolean onTouch(View v, MotionEvent event) { - int action = event.getAction(); - switch (action) { - case MotionEvent.ACTION_DOWN: - case MotionEvent.ACTION_MOVE: - BannerViewPager.this.setLooping(true); - BannerViewPager.this.stopLoop(); - break; - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_CANCEL: - BannerViewPager.this.setLooping(false); - BannerViewPager.this.startLoop(); - default: - break; - } - return false; - } - }); - } - @Override public void onPageSelected(int position) { // Optimized For Issue #42 @@ -211,19 +186,6 @@ private void initBannerData(List list) { } } -// private void initList(List list) { -// mList.clear(); -// mList.addAll(list); -// mIndicatorView.setPageSize(mList.size()); -// if (mList.size() > 1) { -// setIndicatorValues(); -// } else if (mIndicatorView != null) { -// mIndicatorView.setPageSize(mList.size()); -// } -// setIndicatorValues(list); -// -// } - private void setIndicatorValues(List list) { BannerOptions bannerOptions = mBannerManager.bannerOptions(); bannerOptions.resetIndicatorOptions(); @@ -284,27 +246,25 @@ private void initRoundCorner() { } private void setupViewPager(List list) { - if (holderCreator != null) { - if (list.size() > 0 && isCanLoop()) { - currentPosition = MAX_VALUE / 2 - ((MAX_VALUE / 2) % list.size()) + 1; - } - removeAllViews(); - mViewPager.setAdapter(getPagerAdapter(list)); - mViewPager.setCurrentItem(currentPosition); - mViewPager.removeOnPageChangeListener(this); - mViewPager.addOnPageChangeListener(this); - BannerOptions bannerOptions = mBannerManager.bannerOptions(); - mViewPager.setScrollDuration(bannerOptions.getScrollDuration()); - mViewPager.disableTouchScroll(bannerOptions.isDisableTouchScroll()); - mViewPager.setFirstLayout(true); - addView(mViewPager); - addView(mIndicatorLayout); - initPageStyle(); - startLoop(); -// setTouchListener(); - } else { + if (holderCreator == null) { throw new NullPointerException("You must set HolderCreator for BannerViewPager"); } + if (list.size() > 0 && isCanLoop()) { + currentPosition = MAX_VALUE / 2 - ((MAX_VALUE / 2) % list.size()) + 1; + } + removeAllViews(); + mViewPager.setAdapter(getPagerAdapter(list)); + mViewPager.setCurrentItem(currentPosition); + mViewPager.removeOnPageChangeListener(this); + mViewPager.addOnPageChangeListener(this); + BannerOptions bannerOptions = mBannerManager.bannerOptions(); + mViewPager.setScrollDuration(bannerOptions.getScrollDuration()); + mViewPager.disableTouchScroll(bannerOptions.isDisableTouchScroll()); + mViewPager.setFirstLayout(true); + addView(mViewPager); + addView(mIndicatorLayout); + initPageStyle(); + startLoop(); } private BannerPagerAdapter mBannerPagerAdapter;