CollapsingToolbarLayout flickers while scrolling down

前端 未结 1 1228
南方客
南方客 2021-01-25 12:37

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

相关标签:
1条回答
  • 2021-01-25 12:53

    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")
    
    0 讨论(0)
提交回复
热议问题