Is it possible to disable MotionLayout?
Now with ConstraintLayout beta 2 you can! This is the official way to do it:
motionLayout.getTransition(R.id.yourTransition).setEnable(false)
Recently I've been hacking around MotionLayout a lot, so far I found my trick to disable Motionlayout is like below.
layoutContainer.setTransitionListener(object: MotionLayout.TransitionListener {
override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {}
override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {}
override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {}
override fun onTransitionChange(p0: MotionLayout?, startedScene: Int, endScene: Int, p3: Float) {
when(currentFragmentTag) {
TAG_DEFAULTBACKGROUND -> {
if( startedScene==R.id.halfExpanded ) {
layoutContainer.transitionToState(startedScene)
}
}
}
}
})
So basically, I'm telling to MotionLayout to go back on certain situations.
I feel like onTransitionTrigger
or onTransitionStarted
should contain the stopping function but it won't listen at those moments.
onTransitionChange
is where it actually starts moving.
I think you'll get the point of this trick vice versa.
Happy coding!
I found a handy solution to temporarily disable motion layout transtion. Set this to your MotionLayout in xml :
app:interactionEnabled="false"
The advantage of the solution is using it directly in an xml file, which gives you the option of connecting to data binding
app:interactionEnabled="@{viewModel.shouldInteractionBeActive}"
Its working out of box without any additional binding adapters. This was added in constraintlayout:2.1.0-beta01.
Actually, i managed to get it working by setting the start state of my scene for both start and end. In kotlin:
motionLayout.setTransition(R.id.start, R.id.start)
And when i needed to enable my layout:
motionLayout.setTransition(R.id.start, R.id.end)
No need to change your scene xml, worked flawlessly for me in alpha-03