0

Tutoiral I see with MotionLayout have things in common and that is it focused on either an onClick or onSwipe to trigger MotionLayout Transition. I was wondering how to Transition from something like State A to State B with other trigger like instead of onClick it would trigger onNavDestinationChanged something like:

navController.addNavigationChangeListener { motionLayout.transtionToAnotherState() }

Something similar to TransitionManager where if I want to go from Scene A to Scene B I just do:

TransitionManager.beginDelayedTransition(rootLayout)
// Do someLayout changes

1) Is something similar doable with MotionLayout? 2) If yes can anyone guide me?

Thank you very much.

Archie G. Quiñones
  • 11,638
  • 18
  • 65
  • 107

2 Answers2

1

There is no 100% same way to do it, but you can get similar result if you know your original and final states.

setTransition(R.id.stateA, R.id.stateB);
setTransitionDuration(durationMs); // optional
transitionToEnd(); // here may be transitionToStart to goto state A from B

If you don't know your current state you can read it from TransitionView itself:

getCurrentState();

but be aware, if you are in the middle of transition it will return -1

vadimvolk
  • 711
  • 4
  • 15
  • I did something similar to this. The issue was that state is ruined on rotation. The screen does not update properly after rotation. – Archie G. Quiñones Jun 02 '20 at 11:44
  • Sorry, my app has no landscape mode, but I can guess that ML missing to save it's state in onSaveInstance callback. Did you try to recover proper state yourself? – vadimvolk Jun 02 '20 at 14:56
  • apparently, the one i mentioned above is a bug on the lower version of the library. it is now fixed with 2.0.0-beta06 – Archie G. Quiñones Jun 02 '20 at 15:15
1

So apparently, it was really easy to do this and for anyone else who needs to know simply call:

motionLayout.transitionToState(constraintSetId)
Archie G. Quiñones
  • 11,638
  • 18
  • 65
  • 107