Need to disable expand on CollapsingToolbarLayout for certain fragments

前端 未结 14 1295
萌比男神i
萌比男神i 2020-11-28 08:55

I have a AppCompatActivity that controls replacing many fragments. Here is my layout for it.

activity_main.xml



        
14条回答
  •  情深已故
    2020-11-28 09:42

    The following code achieves 3 objectives:

    Disable CollapsingToolbarLayout expand or collapse by the user, but still allow AppBarLayout.setExpanded.

    Prevent scrolling of RecyclerView or NestedScrollView from expanding or collapsing the CollapsingToolbarLayout.

    // scrollView can be RecyclerView or NestedScrollView
    ViewCompat.setNestedScrollingEnabled(scrollView, false)
    

    Prevent the user from expanding or collapsing the CollapsingToolbarLayout by flicking the AppBar.

    val params = appBar.layoutParams as CoordinatorLayout.LayoutParams
    if (params.behavior == null)
        params.behavior = AppBarLayout.Behavior()
    val behaviour = params.behavior as AppBarLayout.Behavior
    behaviour.setDragCallback(object : AppBarLayout.Behavior.DragCallback() {
        override fun canDrag(appBarLayout: AppBarLayout): Boolean {
            return false
        }
    })
    

    https://code.luasoftware.com/tutorials/android/how-to-disable-or-lock-collapsingtoolbarlayout-collapse-or-expand/

提交回复
热议问题