diff --git a/build.gradle b/build.gradle index afc9df5..39f7949 100755 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:0.7.+' + classpath 'com.android.tools.build:gradle:0.12.2' } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 9b8ffdd..1e61d1f 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/library/build.gradle b/library/build.gradle index 643308e..0185176 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -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' diff --git a/library/src/main/java/fr/castorflex/android/verticalviewpager/VerticalViewPager.java b/library/src/main/java/fr/castorflex/android/verticalviewpager/VerticalViewPager.java index 8fbc9c3..78a42b9 100644 --- a/library/src/main/java/fr/castorflex/android/verticalviewpager/VerticalViewPager.java +++ b/library/src/main/java/fr/castorflex/android/verticalviewpager/VerticalViewPager.java @@ -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; @@ -98,7 +97,7 @@ public float getInterpolation(float t) { } }; - private final ArrayList mItems = new ArrayList(); + private final ArrayList mItems = new ArrayList<>(); private final ItemInfo mTempItem = new ItemInfo(); private final Rect mTempRect = new Rect(); @@ -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; @@ -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); @@ -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); @@ -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--; @@ -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; } @@ -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); @@ -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; } @@ -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; @@ -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); @@ -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 @@ -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) { @@ -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) { @@ -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) { @@ -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; diff --git a/sample/build.gradle b/sample/build.gradle index ef7c773..61f398e 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -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 + } } diff --git a/sample/src/main/java/fr/castorflex/android/verticalviewpager/sample/MainActivity.java b/sample/src/main/java/fr/castorflex/android/verticalviewpager/sample/MainActivity.java index 648db98..439ef74 100644 --- a/sample/src/main/java/fr/castorflex/android/verticalviewpager/sample/MainActivity.java +++ b/sample/src/main/java/fr/castorflex/android/verticalviewpager/sample/MainActivity.java @@ -3,16 +3,20 @@ import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; +import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.TextView; +import android.widget.ArrayAdapter; +import android.widget.ListView; -import java.util.Locale; +import java.util.ArrayList; +import java.util.List; import fr.castorflex.android.verticalviewpager.VerticalViewPager; @@ -28,8 +32,10 @@ protected void onCreate(Bundle savedInstanceState) { VerticalViewPager verticalViewPager = (VerticalViewPager) findViewById(R.id.verticalviewpager); verticalViewPager.setAdapter(new DummyAdapter(getFragmentManager())); - verticalViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.pagemargin)); - verticalViewPager.setPageMarginDrawable(new ColorDrawable(getResources().getColor(android.R.color.holo_green_dark))); + verticalViewPager.setPageMargin(getResources(). + getDimensionPixelSize(R.dimen.pagemargin)); + verticalViewPager.setPageMarginDrawable(new ColorDrawable( + getResources().getColor(android.R.color.holo_green_dark))); verticalViewPager.setPageTransformer(true, new ViewPager.PageTransformer() { @Override @@ -70,44 +76,57 @@ public void transformPage(View view, float position) { } public class DummyAdapter extends FragmentPagerAdapter { + List fragments = new ArrayList<>(); public DummyAdapter(FragmentManager fm) { super(fm); + + for (int i = 0; i < 5; i++) { + fragments.add(PlaceholderFragment.newInstance(i)); + } } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). - return PlaceholderFragment.newInstance(position + 1); + //return PlaceholderFragment.newInstance(position + 1); + return fragments.get(position); } @Override public int getCount() { // Show 3 total pages. - return 3; + return 5; } @Override - public CharSequence getPageTitle(int position) { - Locale l = Locale.getDefault(); + public CharSequence getPageTitle(int position) { switch (position) { case 0: - return "PAGE 1"; + return "PAGE 0"; case 1: - return "PAGE 2"; + return "PAGE 1"; case 2: + return "PAGE 2"; + case 3: return "PAGE 3"; + case 4: + return "PAGE 4"; } return null; } - } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { + String[] array = new String[]{"Android 1", "Android 2", "Android 3", + "Android 4", "Android 5", "Android 6", "Android 7", "Android 8", + "Android 9", "Android 10", "Android 11", "Android 12", "Android 13", + "Android 14", "Android 15", "Android 16"}; + /** * The fragment argument representing the section number for this * fragment. @@ -132,13 +151,36 @@ public PlaceholderFragment() { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View rootView = inflater.inflate(R.layout.fragment_layout, container, false); - TextView textView = (TextView) rootView.findViewById(R.id.textview); - textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); - return rootView; - } + final View rootView = inflater.inflate(R.layout.fragment_layout, container, false); + Log.d("Debug", "creating fragment " + + getArguments().getInt(ARG_SECTION_NUMBER)); - } + switch (getArguments().getInt(ARG_SECTION_NUMBER)) { + case 0: + break; + + case 1: + rootView.setBackgroundColor(Color.BLACK); + break; + + case 2: + rootView.setBackgroundColor(Color.BLUE); + break; + + case 3: + rootView.setBackgroundColor(Color.GREEN); + break; + case 4: + rootView.setBackgroundColor(Color.RED); + break; + } + final ListView listView = (ListView) rootView.findViewById(R.id.listView); + listView.setAdapter(new ArrayAdapter<>(getActivity(), + R.layout.list_item, R.id.text1, array)); + + return rootView; + } + } } diff --git a/sample/src/main/res/layout/fragment_layout.xml b/sample/src/main/res/layout/fragment_layout.xml index 1cf8f2c..970067d 100644 --- a/sample/src/main/res/layout/fragment_layout.xml +++ b/sample/src/main/res/layout/fragment_layout.xml @@ -1,17 +1,14 @@ - - + \ No newline at end of file diff --git a/sample/src/main/res/layout/list_item.xml b/sample/src/main/res/layout/list_item.xml new file mode 100644 index 0000000..74cce88 --- /dev/null +++ b/sample/src/main/res/layout/list_item.xml @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/sample/src/main/res/values/styles.xml b/sample/src/main/res/values/styles.xml new file mode 100644 index 0000000..ff6c9d2 --- /dev/null +++ b/sample/src/main/res/values/styles.xml @@ -0,0 +1,8 @@ + + + + + +