2

I have to add MotionLayout to different layouts in android application. Since the app i am working on is a little bit old it has a lot of complex, nested layouts. And there is a rule that kills me:

MotionLayout works only with its direct children

I red that before maybe there were some options with NestedScrollView when android used older support v4 library? Can CoordinatorLayout Behavior be applied only on direct children of CoordinatorLayout?

Is there any hack that can help me in achieving this without changing all my layouts since they are huge and changing them will affect my app and probably take days to keep same behaviour as before.

2 Answers2

2

Is there any hack that can help me in achieving this without changing all my layouts since they are huge

yes there is a way to do it and it's not like a hack, it just involves some few lines of code. I already answered a question like this one in this link.

The resume is, you can create multiple scenes: enter image description here

link them using MotionLayout.TransitionListener and a function like this one:

private fun updateNestedMotionLayout(motionLayout: MotionLayout?) = motionLayout?.let {
    with(binding) {
        if (it.id == rootContainer.id) {
            headerContainer.progress = it.progress
            childHeaderContainer.progress = it.progress
        }
    }
}

Check out the whole example here, probably this can be a good starting point for you.

MiguelHincapieC
  • 5,445
  • 7
  • 41
  • 72
1

No good answers for you. Fundamentally MotionLayout as a Layout (subclass of ViewGroup). ViewGroup is responsible for the positioning of there children. If a ViewGroup tried to control another viewgroup's child the two would conflict.

If its is a fire and forget animation then you could Try TransitionManager. It sidesteps the layout system for the duration of the animation.

hoford
  • 4,918
  • 2
  • 19
  • 19
  • Yes I understand. Nice explanation about ViewGroup positioning of its children and conflicts. Thanks for your answer –  Dec 26 '20 at 11:09