Skip to content

Commit

Permalink
Optimize BannerViewPager
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip committed Dec 20, 2019
1 parent 0fdb48d commit d13fa71
Showing 1 changed file with 18 additions and 58 deletions.
76 changes: 18 additions & 58 deletions bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.zhpan.bannerview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
Expand Down Expand Up @@ -115,7 +114,7 @@ protected void onAttachedToWindow() {
startLoop();
}

//触碰控件的时候,翻页应该停止,离开的时候如果之前是开启了翻页的话则重新启动翻页
// 触碰控件的时候,翻页应该停止,离开的时候如果之前是开启了翻页的话则重新启动翻页
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = ev.getAction();
Expand All @@ -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
Expand Down Expand Up @@ -211,19 +186,6 @@ private void initBannerData(List<T> list) {
}
}

// private void initList(List<T> 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<T> list) {
BannerOptions bannerOptions = mBannerManager.bannerOptions();
bannerOptions.resetIndicatorOptions();
Expand Down Expand Up @@ -284,27 +246,25 @@ private void initRoundCorner() {
}

private void setupViewPager(List<T> 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<T, VH> mBannerPagerAdapter;
Expand Down

0 comments on commit d13fa71

Please sign in to comment.