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

Fix x/y translation misses from ViewPager.java #18

Open
wants to merge 7 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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
classpath 'com.android.tools.build:gradle:0.12.2'
}
}

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=http\://services.gradle.org/distributions/gradle-1.12-all.zip
29 changes: 19 additions & 10 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
apply plugin: 'android-library'
apply plugin: 'com.android.library'

repositories {
mavenCentral()
}

dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile 'com.android.support:support-v4:20.0.0'
}

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
compileSdkVersion 20
buildToolsVersion "20.0.0"

defaultConfig {
minSdkVersion 4
targetSdkVersion 19
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)
applicationId "library"
minSdkVersion 14
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}

apply from: '../mvn_push.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
* Just a copy of the original ViewPager modified to support vertical Scrolling
*/
public class VerticalViewPager extends ViewGroup {

private static final String TAG = "ViewPager";
private static final boolean DEBUG = false;

Expand Down Expand Up @@ -98,7 +97,7 @@ public float getInterpolation(float t) {
}
};

private final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
private final ArrayList<ItemInfo> mItems = new ArrayList<>();
private final ItemInfo mTempItem = new ItemInfo();

private final Rect mTempRect = new Rect();
Expand Down Expand Up @@ -324,7 +323,7 @@ public void setAdapter(PagerAdapter adapter) {
final boolean wasFirstLayout = mFirstLayout;
mFirstLayout = true;
mExpectedAdapterCount = mAdapter.getCount();
if (mRestoredCurItem >= 0) {
if (mRestoredCurItem >= 0 && mRestoredAdapterState != null && mRestoredClassLoader != null) {
mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader);
setCurrentItemInternal(mRestoredCurItem, false, true);
mRestoredCurItem = -1;
Expand Down Expand Up @@ -704,7 +703,7 @@ void smoothScrollTo(int x, int y, int velocity) {

final int height = getClientHeight();
final int halfHeight = height / 2;
final float distanceRatio = Math.min(1f, 1.0f * Math.abs(dx) / height);
final float distanceRatio = Math.min(1f, 1.0f * Math.abs(dy) / height);
final float distance = halfHeight + halfHeight *
distanceInfluenceForSnapDuration(distanceRatio);

Expand All @@ -714,7 +713,7 @@ void smoothScrollTo(int x, int y, int velocity) {
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);
final float pageDelta = (float) Math.abs(dy) / (pageHeight + mPageMargin);
duration = (int) ((pageDelta + 1) * 100);
}
duration = Math.min(duration, MAX_SETTLE_DURATION);
Expand Down Expand Up @@ -899,7 +898,7 @@ void populate(int newCurrentItem) {
mAdapter.destroyItem(this, pos, ii.object);
if (DEBUG) {
Log.i(TAG, "populate() - destroyItem() with pos: " + pos +
" view: " + ((View) ii.object));
" view: " + ii.object);
}
itemIndex--;
curIndex--;
Expand Down Expand Up @@ -933,7 +932,7 @@ void populate(int newCurrentItem) {
mAdapter.destroyItem(this, pos, ii.object);
if (DEBUG) {
Log.i(TAG, "populate() - destroyItem() with pos: " + pos +
" view: " + ((View) ii.object));
" view: " + ii.object);
}
ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
}
Expand Down Expand Up @@ -1734,17 +1733,9 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
final float yDiff = Math.abs(dy);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float xDiff = Math.abs(x - mInitialMotionX);
if (DEBUG) Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
if (DEBUG) Log.v(TAG, "Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

if (dy != 0 && !isGutterDrag(mLastMotionY, dy) &&
canScroll(this, false, (int) dy, (int) x, (int) y)) {
// Nested view has scrollable area under this point. Let it be handled there.
mLastMotionX = x;
mLastMotionY = y;
mIsUnableToDrag = true;
return false;
}
if (yDiff > mTouchSlop && yDiff * 0.5f > xDiff) {
if (dy != 0 && (allowDragDown(dy) || allowDragUp(dy)) && !canScroll(this, true, (int) dy, (int) x, (int) y)) {
if (DEBUG) Log.v(TAG, "Starting drag!");
mIsBeingDragged = true;
requestParentDisallowInterceptTouchEvent(true);
Expand Down Expand Up @@ -1797,7 +1788,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {

if (DEBUG) Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY
+ " mIsBeingDragged=" + mIsBeingDragged
+ "mIsUnableToDrag=" + mIsUnableToDrag);
+ " mIsUnableToDrag=" + mIsUnableToDrag);
break;
}

Expand Down Expand Up @@ -1866,7 +1857,7 @@ public boolean onTouchEvent(MotionEvent ev) {
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float xDiff = Math.abs(x - mLastMotionX);
if (DEBUG)
Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
Log.v(TAG, "Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
if (yDiff > mTouchSlop && yDiff > xDiff) {
if (DEBUG) Log.v(TAG, "Starting drag!");
mIsBeingDragged = true;
Expand Down Expand Up @@ -1992,7 +1983,7 @@ private boolean performDrag(float y) {
scrollY = bottomBound;
}
// Don't lose the rounded component
mLastMotionX += scrollY - (int) scrollY;
mLastMotionY += scrollY - (int) scrollY;
scrollTo(getScrollX(), (int) scrollY);
pageScrolled((int) scrollY);

Expand Down Expand Up @@ -2366,6 +2357,20 @@ protected boolean canScroll(View v, boolean checkV, int dy, int x, int y) {
return checkV && ViewCompat.canScrollVertically(v, -dy);
}

/**
* Return false if:
* If the adapter is in the first row
* & (If the child is a viewgroup) If the child is in the first row
* & If the scroll is trying to go up
*/
protected boolean allowDragDown(float dy) {
return mCurItem != 0 && dy > 0;
}

protected boolean allowDragUp(float dy) {
return mCurItem != getAdapter().getCount() - 1 && dy < 0;
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
// Let the focused view and/or our descendants get the key first
Expand Down Expand Up @@ -2439,8 +2444,8 @@ public boolean arrowScroll(int direction) {
direction);
if (nextFocused != null && nextFocused != currentFocused) {
if (direction == View.FOCUS_UP) {
// If there is nothing to the left, or this is causing us to
// jump to the right, then what we really want to do is page left.
// If there is nothing above, or this is causing us to
// jump down, then what we really want to do is page up.
final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
if (currentFocused != null && nextTop >= currTop) {
Expand All @@ -2449,8 +2454,8 @@ public boolean arrowScroll(int direction) {
handled = nextFocused.requestFocus();
}
} else if (direction == View.FOCUS_DOWN) {
// If there is nothing to the right, or this is causing us to
// jump to the left, then what we really want to do is page right.
// If there is nothing below, or this is causing us to
// jump down, then what we really want to do is page down.
final int nextDown = getChildRectInPagerCoordinates(mTempRect, nextFocused).bottom;
final int currDown = getChildRectInPagerCoordinates(mTempRect, currentFocused).bottom;
if (currentFocused != null && nextDown <= currDown) {
Expand All @@ -2460,10 +2465,10 @@ public boolean arrowScroll(int direction) {
}
}
} else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
// Trying to move left and nothing there; try to page.
// Trying to move up and nothing there; try to page.
handled = pageUp();
} else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
// Trying to move right and nothing there; try to page.
// Trying to move down and nothing there; try to page.
handled = pageDown();
}
if (handled) {
Expand Down Expand Up @@ -2742,7 +2747,7 @@ public static class LayoutParams extends ViewGroup.LayoutParams {
public int gravity;

/**
* Width as a 0-1 multiplier of the measured pager width
* Height as a 0-1 multiplier of the measured pager height
*/
float heightFactor = 0.f;

Expand Down
39 changes: 15 additions & 24 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
apply plugin: 'android'
apply plugin: 'com.android.application'

repositories {
mavenCentral()
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v13:20.0.0'
compile project(':library')
compile 'com.android.support:support-v13:19.0.0'
}

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
compileSdkVersion 20
buildToolsVersion '20.0.0'

defaultConfig {
applicationId "mobileplus.verticalviewpager"
minSdkVersion 14
targetSdkVersion 19
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)
}

signingConfigs {
release
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
signingConfig project.hasProperty('storeFile') ? signingConfigs.release : signingConfigs.debug
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
if (project.hasProperty('storeFile')) {
android.signingConfigs.release.storeFile = file(storeFile)
}
if (project.hasProperty('keyAlias')) {
android.signingConfigs.release.keyAlias = keyAlias
}
if (project.hasProperty('storePassword')) {
android.signingConfigs.release.storePassword = storePassword
}
if (project.hasProperty('keyPassword')) {
android.signingConfigs.release.keyPassword = keyPassword
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
Loading