Gnzlt
Gnzlt

Reputation: 4572

MotionLayout setTransition() always calls transitionToStart()

Using MotionLayout, I need to temporarily transition to a loading state (ConstraintSet), which I do by calling:

motionLayout.transitionToState(R.id.state_loading)

Then I want to transition back to the default Transition declared in my MotionScene XML:

motionLayout.transitionToState(R.id.state_expanded)

The problem comes now when I want to restore the default behavior by setting an endState calling:

motionLayout.setTransition(R.id.state_expanded, R.id.state_collapsed)

The issue here is that setTransition(int beginId, int endId) also calls transitionToStart(), animating it again in a weird manner.

So my question is if there is any way to call setTransition() without calling transitionToStart()?

Upvotes: 3

Views: 1485

Answers (1)

Gnzlt
Gnzlt

Reputation: 4572

I found an ugly way to do this by creating a custom MotionLayout, just to make setTransition(Transition transition) method public (since it's protected but it won't call transitionToStart()):

class CustomMotionLayout : MotionLayout {

    constructor(context: Context) : super(context)

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr)

    public override fun setTransition(transition: MotionScene.Transition) {
        super.setTransition(transition)
    }
}

Then retrieving your Transition by Id and passing it to the new public method:

val desiredTransition = customMotionLayout.getTransition(R.id.transition_default)
customMotionLayout.setTransition(desiredTransition)

This way, when setting a Transition programmatically to a MotionLayout, it won't call transitionToStart().

Upvotes: 1

Related Questions