Skip to content

Instantly share code, notes, and snippets.

@kmansoft
Created November 19, 2015 19:00
Show Gist options
  • Save kmansoft/e70ecacec282ccd443e0 to your computer and use it in GitHub Desktop.
Save kmansoft/e70ecacec282ccd443e0 to your computer and use it in GitHub Desktop.
ViewPager patch to allow the adapter not know the exact number of items, only two adjoining ones
/*
This patch lets a ViewPagerAdapter{subclass} not know the exact number of items, and only keep prev and next.
- Return some arbitrary large number from getCount
- Maintain prev and next data items (not whole list)
- In instantiateItem, return null for item positions other than prev/next, or when there is no prev/next item
With this patch, ViewPager will notice the "null"'s and treat them like "end of list" markers.
*/
diff -r dfe398558897 AquaSupport/src/android/support/v4/view/ViewPagerEx.java
--- a/AquaSupport/src/android/support/v4/view/ViewPagerEx.java Thu Nov 19 17:56:39 2015 +0300
+++ b/AquaSupport/src/android/support/v4/view/ViewPagerEx.java Thu Nov 19 21:55:58 2015 +0300
@@ -1311,7 +1311,22 @@
float offset = curItem.offset;
int pos = curItem.position - 1;
mFirstOffset = curItem.position == 0 ? curItem.offset : -Float.MAX_VALUE;
- mLastOffset = curItem.position == N - 1 ? curItem.offset + curItem.widthFactor - 1 : Float.MAX_VALUE;
+ // kman begin
+ if (curItem.position == 0) {
+ mFirstOffset = curItem.offset;
+ } else if (curIndex > 0 && mItems.get(curIndex - 1).object == null) {
+ mFirstOffset = curItem.offset;
+ } else {
+ mFirstOffset = -Float.MAX_VALUE;
+ }
+ if (curItem.position == N - 1) {
+ mLastOffset = curItem.offset + curItem.widthFactor - 1;
+ } else if (curIndex < itemCount - 1 && mItems.get(curIndex + 1).object == null) {
+ mLastOffset = curItem.offset + curItem.widthFactor - 1;
+ } else {
+ mLastOffset = Float.MAX_VALUE;
+ }
+ // kman end
// Previous pages
for (int i = curIndex - 1; i >= 0; i--, pos--) {
final ItemInfo ii = mItems.get(i);
@@ -2220,10 +2235,22 @@
if (firstItem.position != 0) {
leftAbsolute = false;
leftBound = firstItem.offset * width;
+ // kman begin
+ if (firstItem.object == null) {
+ leftAbsolute = true;
+ leftBound = width * mFirstOffset;
+ }
+ // kman end
}
if (lastItem.position != mAdapter.getCount() - 1) {
rightAbsolute = false;
rightBound = lastItem.offset * width;
+ // kman begin
+ if (lastItem.object == null) {
+ rightAbsolute = true;
+ rightBound = width * mLastOffset;
+ }
+ // kman end
}
if (scrollX < leftBound) {
@@ -2308,6 +2335,16 @@
// Only let the user target pages we have items for
targetPage = Math.max(firstItem.position, Math.min(targetPage, lastItem.position));
+
+ // kman
+ if (lastItem.position == targetPage && lastItem.object == null) {
+ targetPage = currentPage;
+ } else if (firstItem.position == targetPage && firstItem.object == null) {
+ targetPage = currentPage;
+ }
+ } else {
+ // kman
+ targetPage = currentPage;
}
return targetPage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment