I have a detail page which includes CollapsingToolbarLayout and NestedScrollView in CoordinatorLayout. When I scroll up from CollapsingToolbarLayout and scroll down from NestedS
The reason behind this is because AppBarLayout
does not stop fling when we start fling on any other view of CoordinatorLayout
. Solution is quite simple: whenever any child view of CoordinatorLayout
starts fling, we need to stop the fling of AppBarLayout
(code below is for androidx)
class CustomAppBarBehavior : AppBarLayout.Behavior() {
private var overScroller: OverScroller? = null
override fun onNestedPreFling(coordinatorLayout: CoordinatorLayout,
child: AppBarLayout,
target: View,
velocityX: Float,
velocityY: Float): Boolean {
stopAppBarLayoutFling()
return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY)
}
private fun stopAppBarLayoutFling() {
if (overScroller == null) {
val scrollerField = javaClass.superclass.superclass.superclass.getDeclaredField("scroller")
scrollerField.isAccessible = true
overScroller = scrollerField.get(this) as? OverScroller
}
overScroller?.forceFinished(true)
}
}
In support version 27, finding the scroller via reflection is a bit different:
val scrollerField = javaClass.superclass.superclass.getDeclaredField("mScroller")