Skip to content Skip to sidebar Skip to footer

Collapsingtoolbarlayout Flickers While Scrolling Down

I have a detail page which includes CollapsingToolbarLayout and NestedScrollView in CoordinatorLayout. When I scroll up from CollapsingToolbarLayout and scroll down from NestedScro

Solution 1:

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)

classCustomAppBarBehavior : AppBarLayout.Behavior() {

    privatevar overScroller: OverScroller? = nulloverridefunonNestedPreFling(coordinatorLayout: CoordinatorLayout,
                                  child: AppBarLayout,
                                  target: View,
                                  velocityX: Float,
                                  velocityY: Float): Boolean {
        stopAppBarLayoutFling()
        returnsuper.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY)
    }

    privatefunstopAppBarLayoutFling() {
        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")

Post a Comment for "Collapsingtoolbarlayout Flickers While Scrolling Down"