How To Disable Or Lock CollapsingToolbarLayout Collapse Or Expand

I can't use null for AppBarLayout.scrollFlags because I want to expand and collapse AppBarLayout by code (AppBarLayout.setExpanded). I just don't to allow users' interactions to expand or collapse the CollapsingToolbarLayout/AppBarLayout.

Use the following code to prevent scrolling of RecyclerView or NestedScrollView from expanding or collapsing the CollapsingToolbarLayout.

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

User still can expand or collapse CollapsingToolbarLayout by flicking the AppBarLayout. To prevent this from happening, use the following code.

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

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.