Android collapsing toolbar with MotionLayout - disable motion when RecyclerView is empty/not scrollable
Asked Answered
J

3

11

I'm trying to use the MotionLayout View to get a collapsing toolbar behavior. My approach is similar to the example here: https://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part-1/

This works fine, but the transition also starts on swipe, even if the recyclerview is empty or has fewer entries than would fit on the screen.

Is there any way to only enable the MotionLayout transition, if the recycler is actually able to scroll?

My OnSwipe description:

<OnSwipe
    app:dragDirection="dragUp"
    app:maxAcceleration="40"
    app:moveWhenScrollAtTop="true"
    app:touchAnchorId="@id/recycler"
    app:touchAnchorSide="top" />
Johnstone answered 2/6, 2019 at 10:8 Comment(2)
any update on this? find a way to disable the transition? I tried getTransition().setEnabled(false) with no successNarcoanalysis
I've been trying the same thing for a day now, with no luck. If you did manage, please share :)Stewardson
P
0
if (dataList.isEmpty()) {
 motionLayout.enableTransition(R.id.transition, false)
}
Pantaloon answered 17/7, 2020 at 6:29 Comment(0)
E
-1

Call the below function after populating the recycler view

     private fun enableOrDisableMotionLayout() {
                    Handler(Looper.getMainLooper()).postDelayed({
                        try {
                            if (binding.recyclerActivity != null) {
                                if (binding.recyclerActivity.canScrollVertically(-1) || binding.recyclerActivity.canScrollVertically(
                                        1
                                    )
                                ) {
                                    //R.id.transition should be the id used in Transition Tag in MotionScene xml file
                                    binding.motionLayout.enableTransition(R.id.transition, true)
                                } else {
                                    
                                    binding.motionLayout.enableTransition(R.id.transition, false)
                                }
                            }
                        } catch (e: Exception) {
            
                        }
                    }, 50L)
                }

Above code will disable or Enable motionLayout scrolling based on content inside recyclerview.. Note : Still not found the code to stop the scrolling od motionlayout when the last item is partially visible which makes the motionlayout to scroll to collapsed state even all content of recycler view is showwn

Euthanasia answered 29/9, 2021 at 12:54 Comment(0)
T
-3

Change your OnSwipe like this:

<OnSwipe
      app:dragDirection="dragUp"
      app:touchAnchorId="@id/recycler"
      app:touchAnchorSide="top" />
Tedda answered 2/6, 2019 at 10:11 Comment(1)
Sorry, this does not work. Still the same behavior.Johnstone

© 2022 - 2024 — McMap. All rights reserved.