Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add velocity control #35

Open
wants to merge 4 commits into
base: master
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
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
buildscript {
ext.kotlin_version = '1.2.21'
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
classpath 'com.android.tools.build:gradle:2.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

Expand Down
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
VERSION_NAME=19.0.1
VERSION_CODE=2
GROUP=com.github.castorflex.verticalviewpager
androidMinSdkVersion=14
androidTargetSdkVersion=27
androidCompileSdkVersion=27
androidBuildToolsVersion=25.0.3

#storeFile=nice try
#keyAlias=nice try
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.9-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
15 changes: 11 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
apply plugin: 'android-library'
apply plugin: 'kotlin-android'

repositories {
mavenCentral()
}

dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
def androidCompileSdkVersion = project.androidCompileSdkVersion.toInteger()
def androidTargetSdkVersion = project.androidTargetSdkVersion.toInteger()
def androidMinSdkVersion = project.androidMinSdkVersion.toInteger()
def androidBuildToolsVersion = project.androidBuildToolsVersion.toString()

compileSdkVersion androidCompileSdkVersion
buildToolsVersion androidBuildToolsVersion

defaultConfig {
minSdkVersion 4
targetSdkVersion 19
minSdkVersion androidMinSdkVersion
targetSdkVersion androidTargetSdkVersion
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,27 @@ public void setCurrentItem(int item, boolean smoothScroll) {
setCurrentItemInternal(item, smoothScroll, false);
}

/**
* Set the currently selected page with duration.
*
* @param item Item index to select
* @param duration Scroll duration to reach item index
*/
public void setCurrentItem(int item, int duration) {
mPopulatePending = false;
setCurrentItemInternal(item, true, false, 0, duration);
}


public int getCurrentItem() {
return mCurItem;
}

void setCurrentItemInternal(int item, boolean smoothScroll, boolean always) {
setCurrentItemInternal(item, smoothScroll, always, 0);
setCurrentItemInternal(item, smoothScroll, always, 0, 0);
}

void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) {
void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity, int duration) {
if (mAdapter == null || mAdapter.getCount() <= 0) {
setScrollingCacheEnabled(false);
return;
Expand Down Expand Up @@ -445,12 +457,12 @@ void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int
requestLayout();
} else {
populate(item);
scrollToItem(item, smoothScroll, velocity, dispatchSelected);
scrollToItem(item, smoothScroll, velocity, dispatchSelected, duration);
}
}

private void scrollToItem(int item, boolean smoothScroll, int velocity,
boolean dispatchSelected) {
boolean dispatchSelected, int duration) {
final ItemInfo curInfo = infoForPosition(item);
int destY = 0;
if (curInfo != null) {
Expand All @@ -459,7 +471,7 @@ private void scrollToItem(int item, boolean smoothScroll, int velocity,
Math.min(curInfo.offset, mLastOffset)));
}
if (smoothScroll) {
smoothScrollTo(0, destY, velocity);
smoothScrollTo(0, destY, velocity, duration);
if (dispatchSelected && mOnPageChangeListener != null) {
mOnPageChangeListener.onPageSelected(item);
}
Expand Down Expand Up @@ -672,7 +684,7 @@ float distanceInfluenceForSnapDuration(float f) {
* @param y the number of pixels to scroll by on the Y axis
*/
void smoothScrollTo(int x, int y) {
smoothScrollTo(x, y, 0);
smoothScrollTo(x, y, 0,0);
}

/**
Expand All @@ -682,7 +694,7 @@ void smoothScrollTo(int x, int y) {
* @param y the number of pixels to scroll by on the Y axis
* @param velocity the velocity associated with a fling, if applicable. (0 otherwise)
*/
void smoothScrollTo(int x, int y, int velocity) {
void smoothScrollTo(int x, int y, int velocity, int duration) {
if (getChildCount() == 0) {
// Nothing to do.
setScrollingCacheEnabled(false);
Expand All @@ -708,18 +720,20 @@ void smoothScrollTo(int x, int y, int velocity) {
final float distance = halfHeight + halfHeight *
distanceInfluenceForSnapDuration(distanceRatio);

int duration = 0;
velocity = Math.abs(velocity);
if (velocity > 0) {
duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
} else {
final float pageHeight = height * mAdapter.getPageWidth(mCurItem);
final float pageDelta = (float) Math.abs(dx) / (pageHeight + mPageMargin);
duration = (int) ((pageDelta + 1) * 100);
int scrollDuration = duration;
if (duration <= 0) {
velocity = Math.abs(velocity);
if (velocity > 0) {
duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
} else {
final float pageHeight = height * mAdapter.getPageWidth(mCurItem);
final float pageDelta = (float) Math.abs(dx) / (pageHeight + mPageMargin);
duration = (int) ((pageDelta + 1) * 100);
}
scrollDuration = Math.min(duration, MAX_SETTLE_DURATION);
}
duration = Math.min(duration, MAX_SETTLE_DURATION);

mScroller.startScroll(sx, sy, dx, dy, duration);
mScroller.startScroll(sx, sy, dx, dy, scrollDuration);
ViewCompat.postInvalidateOnAnimation(this);
}

Expand Down Expand Up @@ -1495,7 +1509,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
mDecorChildCount = decorCount;

if (mFirstLayout) {
scrollToItem(mCurItem, false, 0, false);
scrollToItem(mCurItem, false, 0, false, 0);
}
mFirstLayout = false;
}
Expand Down Expand Up @@ -1911,7 +1925,7 @@ public boolean onTouchEvent(MotionEvent ev) {
final int totalDelta = (int) (y - mInitialMotionY);
int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
totalDelta);
setCurrentItemInternal(nextPage, true, true, initialVelocity);
setCurrentItemInternal(nextPage, true, true, initialVelocity, 0);

mActivePointerId = INVALID_POINTER;
endDrag();
Expand All @@ -1920,7 +1934,7 @@ public boolean onTouchEvent(MotionEvent ev) {
break;
case MotionEvent.ACTION_CANCEL:
if (mIsBeingDragged) {
scrollToItem(mCurItem, true, 0, false);
scrollToItem(mCurItem, true, 0, false, 0);
mActivePointerId = INVALID_POINTER;
endDrag();
needsInvalidate = mTopEdge.onRelease() | mBottomEdge.onRelease();
Expand Down Expand Up @@ -2210,7 +2224,7 @@ public void endFakeDrag() {
final int totalDelta = (int) (mLastMotionY - mInitialMotionY);
int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
totalDelta);
setCurrentItemInternal(nextPage, true, true, initialVelocity);
setCurrentItemInternal(nextPage, true, true, initialVelocity, 0);
endDrag();

mFakeDragging = false;
Expand Down
4 changes: 2 additions & 2 deletions mvn_push.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ afterEvaluate { project ->
}

task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.allJava
source = android.sourceSets.main.java
}

task androidJavadocsJar(type: Jar) {
Expand All @@ -101,7 +101,7 @@ afterEvaluate { project ->

task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.allSource
from android.sourceSets.main.java
}

artifacts {
Expand Down
16 changes: 12 additions & 4 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'android'
apply plugin: 'kotlin-android'

repositories {
mavenCentral()
Expand All @@ -7,15 +8,22 @@ repositories {
dependencies {
compile project(':library')
compile 'com.android.support:support-v13:19.0.0'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"

def androidCompileSdkVersion = project.androidCompileSdkVersion.toInteger()
def androidTargetSdkVersion = project.androidTargetSdkVersion.toInteger()
def androidMinSdkVersion = project.androidMinSdkVersion.toInteger()
def androidBuildToolsVersion = project.androidBuildToolsVersion.toString()

compileSdkVersion androidCompileSdkVersion
buildToolsVersion androidBuildToolsVersion

defaultConfig {
minSdkVersion 14
targetSdkVersion 19
minSdkVersion androidMinSdkVersion
targetSdkVersion androidTargetSdkVersion
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)
}
Expand Down

This file was deleted.

Loading