Slide 1

Slide 1 text

NESTED SCROLLING LIVING LIFE ON THE EDGE +ChrisBanes @chrisbanes

Slide 2

Slide 2 text

User swipes up, collapsing the AppBarLayout and scrolling the list

Slide 3

Slide 3 text

User swipes down, scrolling the list

Slide 4

Slide 4 text

User needs to swipe down again, to expand the AppBarLayout

Slide 5

Slide 5 text

The problem: Flinging does not transfer left over velocity

Slide 6

Slide 6 text

The goal: Flinging transfers velocity when it reaches the end

Slide 7

Slide 7 text

> NESTED SCROLLING: LIVING LIFE ON THE EDGE Nested Scrolling NESTED SCROLLING

Slide 8

Slide 8 text

Nested scrolling views was difficult

Slide 9

Slide 9 text

Nested scrolling views was difficult

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Slide 12

Slide 12 text

Slide 13

Slide 13 text

Slide 14

Slide 14 text

Nested scrolling was created to solve this

Slide 15

Slide 15 text

An event system where scroll events are dispatched up the view hierarchy

Slide 16

Slide 16 text

ScrollView LinearLayout ImageView ListView

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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);

Slide 21

Slide 21 text

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);

Slide 22

Slide 22 text

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);

Slide 23

Slide 23 text

Pre vs Non-Pre

Slide 24

Slide 24 text

Pre vs Non-Pre Pre-scroll events allow a parent to intercept and consume the event

Slide 25

Slide 25 text

Pre vs Non-Pre The view then scrolls the remainder Pre-scroll events allow a parent to intercept and consume the event

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Let’s see how it happens…

Slide 28

Slide 28 text

ACTION_DOWN

Slide 29

Slide 29 text

ACTION_DOWN // Initialise VelocityTracker and add event mVelocityTracker.addMovement(ev); int y = ev.getY(); // 1020 mInitialMotionY = y; mLastMotionY = y; startNestedScroll(SCROLL_AXIS_VERTICAL);

Slide 30

Slide 30 text

ACTION_DOWN startNestedScroll(SCROLL_AXIS_VERTICAL); // Initialise VelocityTracker and add event mVelocityTracker.addMovement(ev); int y = ev.getY(); // 1020 mInitialMotionY = y; mLastMotionY = y;

Slide 31

Slide 31 text

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; }

Slide 32

Slide 32 text

ACTION_DOWN startNestedScroll(SCROLL_AXIS_VERTICAL); @Override void onNestedScrollAccepted(View child, View target, int axes) { // child == LinearLayout // target == ListView // axes == SCROLL_AXIS_VERTICAL }

Slide 33

Slide 33 text

ACTION_MOVE

Slide 34

Slide 34 text

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()

Slide 35

Slide 35 text

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()

Slide 36

Slide 36 text

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, ...)

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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()

Slide 40

Slide 40 text

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 }

Slide 41

Slide 41 text

ACTION_UP

Slide 42

Slide 42 text

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();

Slide 43

Slide 43 text

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();

Slide 44

Slide 44 text

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 }

Slide 45

Slide 45 text

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();

Slide 46

Slide 46 text

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();

Slide 47

Slide 47 text

The problem: Flinging does not transfer left over velocity

Slide 48

Slide 48 text

The problem: Nested flinging does not allow interception like scrolling

Slide 49

Slide 49 text

> NESTED SCROLLING: LIVING LIFE ON THE EDGE Indirect Nested Scrolling INDIRECT NESTED SCROLLING Work in progress

Slide 50

Slide 50 text

Nested scrolling

Slide 51

Slide 51 text

Direct nested scrolling

Slide 52

Slide 52 text

Direct nested scrolling User directly touching screen

Slide 53

Slide 53 text

User not directly touching screen Indirect nested scrolling

Slide 54

Slide 54 text

Settling from fling Setting scroll position programmatically Scrolling from KeyEvents ?

Slide 55

Slide 55 text

API is mostly the same as nested scrolling

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

Fling API is mostly the same as nested scrolling

Slide 59

Slide 59 text

Fling API is mostly the same as nested scrolling Works with nested scrolling

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

startNestedScroll(...) ACTION_DOWN

Slide 62

Slide 62 text

dispatchNestedPreScroll(...) dispatchNestedScroll(...) ACTION_MOVE

Slide 63

Slide 63 text

ACTION_MOVE

Slide 64

Slide 64 text

stopNestedScroll() ACTION_UP fling() startNestedIndirectScroll(...) ↳

Slide 65

Slide 65 text

dispatchNestedIndirectPreScroll(...) dispatchNestedIndirectScroll(...)

Slide 66

Slide 66 text

stopNestedIndirectScroll()

Slide 67

Slide 67 text

Full demo

Slide 68

Slide 68 text

Over and out… +ChrisBanes @chrisbanes