Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Support setting auto scroll speed #126

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
if (buttonView == autoScrollCheckBox) {
if (isChecked) {
ultraViewPager.setAutoScrollSpeed(1000);
SparseIntArray special = new SparseIntArray();
special.put(0, 5000);
special.put(1, 1500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import android.graphics.Bitmap;
import android.util.SparseIntArray;
import android.view.animation.Interpolator;

/**
* Created by mikeafc on 15/11/30.
Expand Down Expand Up @@ -97,6 +98,21 @@ interface IUltraViewPagerFeature {
*/
void setAutoScroll(int intervalInMillis, SparseIntArray intervalArray);

/**
* Set the scroll speed for this ViewPager.
*
* @param duration The default duration to scroll
*/
void setAutoScrollSpeed(int duration);

/**
* Set the scroll speed and interpolator for this ViewPager.
*
* @param duration The default duration to scroll
* @param interpolator The default interpolator to scroll
*/
void setAutoScrollSpeed(int duration, Interpolator interpolator);

/**
* Disable auto-scroll mode
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.widget.RelativeLayout;

/**
Expand Down Expand Up @@ -328,6 +328,16 @@ public void setAutoScroll(int intervalInMillis, SparseIntArray intervalArray) {
startTimer();
}

@Override
public void setAutoScrollSpeed(int duration) {
viewPager.setAutoScrollSpeed(duration);
}

@Override
public void setAutoScrollSpeed(int duration, Interpolator interpolator) {
viewPager.setAutoScrollSpeed(duration, interpolator);
}

@Override
public void disableAutoScroll() {
stopTimer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.tmall.ultraviewpager;

import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

/**
* Created by Jimmy Sun on 19/04/16.
*/
class UltraViewPagerScroller extends Scroller {
private int mDuration = 1000;

UltraViewPagerScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}

@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}

@Override
public void startScroll(int startX, int startY, int dx, int dy) {
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}

/**
* Set the scroll speed.
*
* @param duration The default duration to scroll
*/
void setScrollDuration(int duration) {
mDuration = duration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;

import com.tmall.ultraviewpager.transformer.UltraVerticalTransformer;

import java.lang.reflect.Field;

/**
* Created by mikeafc on 15/11/25.
*/
Expand Down Expand Up @@ -266,6 +268,25 @@ public void setMultiScreen(float ratio) {
}
}

public void setAutoScrollSpeed(int duration) {
setAutoScrollSpeed(duration, null);
}

public void setAutoScrollSpeed(int duration, Interpolator interpolator) {
if (duration >= 0) {
try {
Field mScroller = ViewPager.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
UltraViewPagerScroller scroller = new UltraViewPagerScroller(getContext(),
interpolator);
scroller.setScrollDuration(duration);
mScroller.set(this, scroller);
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void setEnableLoop(boolean status) {
enableLoop = status;
if (pagerAdapter != null) {
Expand Down