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" />
kroegerama
  • 769
  • 9
  • 27

3 Answers3

0
if (dataList.isEmpty()) {
 motionLayout.enableTransition(R.id.transition, false)
}
-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

Jinson Paul
  • 481
  • 6
  • 17
-3

Change your OnSwipe like this:

<OnSwipe
      app:dragDirection="dragUp"
      app:touchAnchorId="@id/recycler"
      app:touchAnchorSide="top" />
Dmytro Ivanov
  • 1,260
  • 1
  • 8
  • 10