Last active
March 23, 2017 04:32
-
-
Save extralam/fd1c32e9cf53be44a14ce30d47107874 to your computer and use it in GitHub Desktop.
AutoScroll ViewPager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.os.Handler; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.animation.DecelerateInterpolator; | |
import android.widget.Scroller; | |
import java.lang.reflect.Field; | |
/** | |
* BannerView Pager - custom auto scroll viewpager | |
* Copyright (c) 2014年 Alan Lam. All rights reserved. | |
* REF: http://stackoverflow.com/questions/8155257/slowing-speed-of-viewpager-controller-in-android | |
*/ | |
public class AutoScrollViewPager extends ViewPager { | |
private static final int SCROLL_DURATION = 1000; | |
private static final int DEFAULT_CIRCULAR_DURATION = 4000; | |
private Handler mHandler = new Handler(); | |
private boolean mAllowSwipe = true; | |
private int mCurrentPos; | |
private int mCircularDuration; | |
private boolean autoplayDisableOnInteraction = true; | |
private Runnable mChangePosRunnable = new Runnable() { | |
@Override | |
public void run() { | |
int newPos = ++mCurrentPos; | |
if (newPos >= getAdapter().getCount()) { | |
newPos = 0; | |
mCurrentPos = newPos; | |
} | |
setCurrentItem(newPos, true); | |
if (mCurrentPos == getAdapter().getCount()-1) { | |
mHandler.postDelayed(mBackToFirstRunnable, SCROLL_DURATION); | |
mCurrentPos = 0; | |
} | |
mHandler.removeCallbacks(mChangePosRunnable); | |
mHandler.postDelayed(mChangePosRunnable, mCircularDuration); | |
} | |
}; | |
private Runnable mBackToFirstRunnable = new Runnable() { | |
@Override | |
public void run() { | |
mHandler.removeCallbacks(mBackToFirstRunnable); | |
setCurrentItem(0, false); | |
} | |
}; | |
public AutoScrollViewPager(Context context) { | |
super(context); | |
setCustomScroller(); | |
} | |
public AutoScrollViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
setCustomScroller(); | |
} | |
public void setAllowSwipe(boolean allow) { | |
mAllowSwipe = allow; | |
} | |
public void startCircular(int durationMillis) { | |
mCurrentPos = 0; | |
mCircularDuration = durationMillis; | |
stopCircular(); | |
mHandler.postDelayed(mChangePosRunnable, mCircularDuration); | |
} | |
/** | |
* Start circular with default duration {@value #DEFAULT_CIRCULAR_DURATION}ms | |
*/ | |
public void startCircular() { | |
startCircular(DEFAULT_CIRCULAR_DURATION); | |
} | |
public void stopCircular() { | |
mHandler.removeCallbacks(mChangePosRunnable); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent arg0) { | |
return mAllowSwipe?super.onTouchEvent(arg0):true; | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent arg0) { | |
return mAllowSwipe?super.onInterceptTouchEvent(arg0):false; | |
} | |
@Override | |
protected void onDetachedFromWindow() { | |
super.onDetachedFromWindow(); | |
stopCircular(); | |
} | |
public void setAutoplayDisableOnInteraction(boolean isAutoplayDisableOnInteraction){ | |
autoplayDisableOnInteraction = isAutoplayDisableOnInteraction; | |
} | |
/** | |
* Use reflection to set our custom Scroller | |
* for longer animation duration | |
*/ | |
private void setCustomScroller() { | |
try { | |
Class<?> viewPager = ViewPager.class; | |
Field scroller = viewPager.getDeclaredField("mScroller"); | |
scroller.setAccessible(true); | |
scroller.set(this, new CustomScroller(getContext())); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public class CustomScroller extends Scroller { | |
public CustomScroller(Context context) { | |
super(context, new DecelerateInterpolator()); | |
} | |
@Override | |
public void startScroll(int startX, int startY, int dx, int dy, | |
int duration) { | |
super.startScroll(startX, startY, dx, dy, SCROLL_DURATION); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment