Don't collapse Toolbar when RecyclerView fits the screen

前端 未结 10 1512
陌清茗
陌清茗 2020-11-30 21:08


I\'ve made an app using Android Design Library, with a Toolbar and TabLayout.
Actually 2 tabs are present, both with 2 RecyclerView, that automati

10条回答
  •  情歌与酒
    2020-11-30 21:22

    I guess it's the best solution. You need to define your custom AppBarLayout behavior:

    class CustomScrollingViewBehavior : AppBarLayout.Behavior {
    
        constructor() : super()
        constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    
        var shouldScroll = true
    
        override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: AppBarLayout, directTargetChild: View, target: View, axes: Int, type: Int): Boolean {
            return shouldScroll && when (target) {
                is NestedScrollView, is ScrollView, is RecyclerView -> {
                    return target.canScrollVertically(1) || target.canScrollVertically(-1)
                }
                else -> super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type)
            }
        }
    }
    

    And then you just need to use it in your layout as attribute of AppBarLayout:

    ...
    
     
    
    ...
    

    That's it.

    Note: custom behavior also supports fully disabling of the scrolling - you just need to set shouldScroll flag to false

    customScrollingViewBehavior.shouldScroll = false
    

提交回复
热议问题