MikeOrtiz / TouchImageView

Adds touch functionality to Android ImageView.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with scrolling for TouchImageView inside AppBarLayout / CollapsableToolbar

ShankarKakumani opened this issue · comments

Scenario:: I have an Activity with TouchImageView(top 60% of the screen) + Grid of GalleryImages ( bottom 40% of screen )

Functionality:: I have to Collapse/Expand the ImageView based on scroll direction. To achieve this I had to place the TouchImageView inside AppBarLayout / CollapsableToolBar

Issue:: When I try to zoom/pinch/scroll the TouchImageView, The AppBarLayout is getting scrolled. Which you can observe in the below Video

expected behavior:: While Interacting with TouchImageView, The AppBar should not be triggered.

The functionality which I am trying to achieve is the same as InstagramGallery.

I have found the solution

setScollable(yourAppBarLayout, false)

fun setScrollable(appBarLayout: AppBarLayout, scrollable: Boolean) {
    val layoutParams = appBarLayout.layoutParams as CoordinatorLayout.LayoutParams
    val behavior = (layoutParams.behavior as? AppBarLayout.Behavior) ?: AppBarLayout.Behavior()
    behavior.setDragCallback(object : AppBarLayout.Behavior.DragCallback() {
        override fun canDrag(appBarLayout: AppBarLayout): Boolean = scrollable
    })
    layoutParams.behavior = behavior
}

or if you want to use via DataBinding

@BindingAdapter("scrollable")
@JvmStatic
fun setScrollable(appBarLayout: AppBarLayout, scrollable: Boolean) {
    val layoutParams = appBarLayout.layoutParams as CoordinatorLayout.LayoutParams
    val behavior = (layoutParams.behavior as? AppBarLayout.Behavior) ?: AppBarLayout.Behavior()
    behavior.setDragCallback(object : AppBarLayout.Behavior.DragCallback() {
        override fun canDrag(appBarLayout: AppBarLayout): Boolean = scrollable
    })
    layoutParams.behavior = behavior
}


        <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/gallery_appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:scrollable="@{false}"
        android:fitsSystemWindows="true"
        app:layout_constraintTop_toTopOf="parent"
        app:toolbarId="@+id/toolbarConstraint">

Thank you for sharing your solution. ❤️
You are very welcome, to add a demo activity with this