@@ -1456,6 +1456,11 @@ boolean isCollapsible() {
1456
1456
return (scrollFlags & SCROLL_FLAG_SCROLL ) == SCROLL_FLAG_SCROLL
1457
1457
&& (scrollFlags & COLLAPSIBLE_FLAGS ) != 0 ;
1458
1458
}
1459
+
1460
+ /** Returns true if the scroll flags are set to snap. */
1461
+ boolean shouldSnapOnScroll () {
1462
+ return (scrollFlags & SCROLL_FLAG_SNAP ) == SCROLL_FLAG_SNAP ;
1463
+ }
1459
1464
}
1460
1465
1461
1466
/**
@@ -1720,7 +1725,7 @@ private void snapToChildIfNeeded(CoordinatorLayout coordinatorLayout, @NonNull T
1720
1725
final LayoutParams lp = (LayoutParams ) offsetChild .getLayoutParams ();
1721
1726
final int flags = lp .getScrollFlags ();
1722
1727
1723
- if (( flags & LayoutParams . FLAG_SNAP ) == LayoutParams . FLAG_SNAP ) {
1728
+ if (lp . shouldSnapOnScroll () ) {
1724
1729
// We're set the snap, so animate the offset to the nearest edge
1725
1730
int snapTop = -offsetChild .getTop ();
1726
1731
int snapBottom = -offsetChild .getBottom ();
@@ -1811,9 +1816,10 @@ public boolean onLayoutChild(
1811
1816
// 3. non-forced pending actions
1812
1817
final int pendingAction = abl .getPendingAction ();
1813
1818
if (savedState != null && (pendingAction & PENDING_ACTION_FORCE ) == 0 ) {
1819
+ final int totalScrollRange = abl .getTotalScrollRange ();
1814
1820
if (savedState .fullyScrolled ) {
1815
1821
// Keep fully scrolled.
1816
- setHeaderTopBottomOffset (parent , abl , -abl . getTotalScrollRange () );
1822
+ setHeaderTopBottomOffset (parent , abl , -totalScrollRange );
1817
1823
} else if (savedState .fullyExpanded ) {
1818
1824
// Keep fully expanded.
1819
1825
setHeaderTopBottomOffset (parent , abl , 0 );
@@ -1826,6 +1832,10 @@ public boolean onLayoutChild(
1826
1832
} else {
1827
1833
offset += Math .round (child .getHeight () * savedState .firstVisibleChildPercentageShown );
1828
1834
}
1835
+ // If snap is set, set the offset to top or bottom.
1836
+ if (((LayoutParams ) child .getLayoutParams ()).shouldSnapOnScroll ()) {
1837
+ offset = -totalScrollRange * (-offset < totalScrollRange / 2 ? 0 : 1 );
1838
+ }
1829
1839
setHeaderTopBottomOffset (parent , abl , offset );
1830
1840
}
1831
1841
} else if (pendingAction != PENDING_ACTION_NONE ) {
0 commit comments