Share
Embed
Transcript
68 slides
-
> NESTED SCROLLING: LIVING LIFE ON THE EDGE Nested Scrolling
NESTED SCROLLING -
Nested scrolling views was difficult <ScrollView> <LinearLayout> <!-- Some content
--> <ListView> <!-- More content which scrolls --> </ListView> </LinearLayout> </ScrollView> -
An event system where scroll events are dispatched up the
view hierarchy -
void setNestedScrollingEnabled(boolean enabled); boolean isNestedScrollingEnabled(); boolean startNestedScroll(int axes); boolean hasNestedScrollingParent();
boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow); boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow); boolean dispatchNestedPreFling(float velocityX, float velocityY); boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed); void stopNestedScroll(); Sending -
void setNestedScrollingEnabled(boolean enabled); boolean isNestedScrollingEnabled(); boolean startNestedScroll(int axes); boolean hasNestedScrollingParent();
boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow); boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow); boolean dispatchNestedPreFling(float velocityX, float velocityY); boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed); void stopNestedScroll(); Sending -
boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow); boolean
dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow); boolean dispatchNestedPreFling(float velocityX, float velocityY); boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed); Sending -
boolean onStartNestedScroll(View child, View target, int nestedScrollAxes); void onNestedScrollAccepted(View child,
View target, int nestedScrollAxes); void onStopNestedScroll(View target); int getNestedScrollAxes(); Receiving void onNestedPreScroll(View target, int dx, int dy, int[] consumed); void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed); boolean onNestedPreFling(View target, float velocityX, float velocityY); boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed); -
boolean onStartNestedScroll(View child, View target, int nestedScrollAxes); void onNestedScrollAccepted(View child,
View target, int nestedScrollAxes); void onStopNestedScroll(View target); int getNestedScrollAxes(); Receiving void onNestedPreScroll(View target, int dx, int dy, int[] consumed); void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed); boolean onNestedPreFling(View target, float velocityX, float velocityY); boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed); -
Receiving void onNestedPreScroll(View target, int dx, int dy, int[] consumed);
void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed); boolean onNestedPreFling(View target, float velocityX, float velocityY); boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed); -
Pre vs Non-Pre Pre-scroll events allow a parent to intercept
and consume the event -
Pre vs Non-Pre The view then scrolls the remainder Pre-scroll
events allow a parent to intercept and consume the event -
Pre vs Non-Pre Scroll events allow the parent to observe
what the view scrolled The view then scrolls the remainder Pre-scroll events allow a parent to intercept and consume the event -
ACTION_DOWN // Initialise VelocityTracker and add event mVelocityTracker.addMovement(ev); int y
= ev.getY(); // 1020 mInitialMotionY = y; mLastMotionY = y; startNestedScroll(SCROLL_AXIS_VERTICAL); -
ACTION_DOWN startNestedScroll(SCROLL_AXIS_VERTICAL); // Initialise VelocityTracker and add event mVelocityTracker.addMovement(ev); int
y = ev.getY(); // 1020 mInitialMotionY = y; mLastMotionY = y; -
ACTION_DOWN startNestedScroll(SCROLL_AXIS_VERTICAL); @Override boolean onStartNestedScroll(View child, View target, int axes)
{ // child == LinearLayout // target == ListView // axes == SCROLL_AXIS_VERTICAL return true; } -
ACTION_DOWN startNestedScroll(SCROLL_AXIS_VERTICAL); @Override void onNestedScrollAccepted(View child, View target, int axes)
{ // child == LinearLayout // target == ListView // axes == SCROLL_AXIS_VERTICAL } -
ACTION_MOVE int[] mScrollConsumed = new int[2]; int y = ev.getY();
// 1010 int dy = mLastMotionY - y; // -10 if (dispatchNestedPreScroll(0, dy, mScrollConsumed, ...)) { dy -= mScrollConsumed[1]; } // INSERT Move children by remaining dy // TODO call dispatchNestedScroll() -
ACTION_MOVE int[] mScrollConsumed = new int[2]; int y = ev.getY();
// 1010 int dy = mLastMotionY - y; // -10 if (dispatchNestedPreScroll(0, dy, mScrollConsumed, ...)) { dy -= mScrollConsumed[1]; } // INSERT Move children by remaining dy // TODO call dispatchNestedScroll() -
ACTION_MOVE @Override void onNestedPreScroll(View target, int dx, int dy, int[]
consumed) { // dx = 0, dy = -10, consumed = int[2] int amountWeConsume = doSomethingWithPreScroll(dy); // consumed[0] = y, consumed[1] = x consumed[1] = amountWeConsume; } dispatchNestedPreScroll(0, dy, mScrollConsumed, ...) -
ACTION_MOVE @Override void onNestedPreScroll(View target, int dx, int dy, int[]
consumed) { // dx = 0, dy = -10, consumed = int[2] int amountWeConsume = doSomethingWithPreScroll(dy); // consumed[0] = y, consumed[1] = x consumed[1] = amountWeConsume; } dispatchNestedPreScroll(0, dy, mScrollConsumed, ...) // -3 -
ACTION_MOVE int[] mScrollConsumed = new int[2]; int y = ev.getY();
// 1010 int dy = mLastMotionY - y; // -10 if (dispatchNestedPreScroll(0, dy, mScrollConsumed, ...)) { dy -= mScrollConsumed[1]; } // INSERT Move children by remaining dy // TODO call dispatchNestedScroll() // -3 remember // dy = -7 -
ACTION_MOVE int[] mScrollConsumed = new int[2]; int y = ev.getY();
// 1010 int dy = mLastMotionY - y; // -10 if (dispatchNestedPreScroll(0, dy, mScrollConsumed, ...)) { dy -= mScrollConsumed[1]; } // INSERT Move children by remaining dy // TODO call dispatchNestedScroll() -
ACTION_MOVE // INSERT Move children by remaining dy dispatchNestedScroll(0, dy,
0, unconsumedY, ...) @Override void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { // Do something if you wish } -
ACTION_UP float velY = ...; if (!dispatchNestedPreFling(0, velY)) { //
Parent hasn't consumed fling, lets fling // and let the parent observe dispatchNestedFling(0, velY, ...); fling(velY); } stopNestedScroll(); -
ACTION_UP float velY = ...; if (!dispatchNestedPreFling(0, velY)) { //
Parent hasn't consumed fling, lets fling // and let the parent observe dispatchNestedFling(0, velY, ...); fling(velY); } stopNestedScroll(); -
ACTION_UP dispatchNestedPreFling(0, velY); @Override boolean onNestedPreFling(View target, float velX, float
velY) { // return true to consume the whole fling, // false to let the view handle it } -
ACTION_UP float velY = ...; if (!dispatchNestedPreFling(0, velY)) { //
Parent hasn't consumed fling, lets fling // and let the parent observe dispatchNestedFling(0, velY, ...); fling(velY); } stopNestedScroll(); -
ACTION_UP float velY = ...; if (!dispatchNestedPreFling(0, velY)) { //
Parent hasn't consumed fling, lets fling // and let the parent observe dispatchNestedFling(0, velY, ...); fling(velY); } stopNestedScroll(); -
> NESTED SCROLLING: LIVING LIFE ON THE EDGE Indirect Nested
Scrolling INDIRECT NESTED SCROLLING Work in progress -
void setNestedIndirectScrollingEnabled(boolean enabled); boolean isNestedIndirectScrollingEnabled(); boolean startNestedIndirectScroll(int axes); boolean dispatchNestedIndirectPreScroll(int
dx, int dy, int[] consumed); boolean dispatchNestedIndirectScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed); boolean hasNestedIndirectScrollingParent(); void stopNestedIndirectScroll(); Sending -
boolean onStartNestedIndirectScroll(View child, View target, int axes); void onNestedIndirectScrollAccepted(View child,
View target, int axes); void onNestedIndirectPreScroll(View target, int dx, int dy, int[] consumed); void onNestedIndirectScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed); void onStopNestedIndirectScroll(View child); Receiving -
Fling API is mostly the same as nested scrolling Works
with nested scrolling