Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with scrolling for TouchImageView inside AppBarLayout / CollapsableToolbar #400

Closed
ShankarKakumani opened this issue Sep 17, 2021 · 3 comments

Comments

@ShankarKakumani
Copy link

ShankarKakumani commented Sep 17, 2021

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.

@hannesa2
Copy link
Collaborator

#314 (comment)

@ShankarKakumani
Copy link
Author

ShankarKakumani commented Sep 17, 2021

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">

@hannesa2
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants