MotionLayout применить loadLayoutDescription

У меня есть 2 различных поведения для MotionLayout. Идея состоит в том, чтобы изменить их в зависимости от состояния приложения. Но когда я настраиваюсь из кода:

if(smth) {
   constraintToolbar.loadLayoutDescription(R.xml.layout_description_1)
} else {
   constraintToolbar.loadLayoutDescription(R.xml.layout_description_2)
}

Ограничения не применяются. Но они применяются после перетаскивания. Есть какой-то обходной путь или это из-за альфа-версии ConstraintLayout 2.0.0? Я использую:

'com.android.support.constraint:constraint-layout:2.0.0-alpha2'

6 ответов

У меня есть переход в MotionScene.

<Transition
    motion:constraintSetStart="@id/start"
    motion:constraintSetEnd="@id/end"
    motion:duration="1000"
    motion:interpolator="linear">

Начальное ограничение не устанавливается после "MotionLayout.loadLayoutDescription". Я добавил:

MotionLayout.setTransition(R.id.start, R.id.end)

и ограничения применяются правильно.

Немного обходного пути, но работает

if(smth) {
   constraintToolbar.loadLayoutDescription(R.xml.layout_description_1)
   constraintToolbar.setTransition(R.id.start1, R.id.end1)
} else {
   constraintToolbar.loadLayoutDescription(R.xml.layout_description_2)
   constraintToolbar.setTransition(R.id.start2, R.id.end2)
}

Вы должны установить транзакции после загрузки макетов.

        if(smth) {
            constraintToolbar.loadLayoutDescription(R.xml.layout_description_1)
            motionContainer.setTransition(R.id.start1 ,R.id.end1)

        } else {
            constraintToolbar.loadLayoutDescription(R.xml.layout_description_2)
            motionContainer.setTransition(R.id.start1, R.id.end1)
        }

loadLayoutDescription Я думаю, что просто использовать в androidx

implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'

или если вы не используете androidx, вы должны изменить constraintToolbar на движение

val motion = MotionLayout(this)

motion.loadLayoutDescription

после звонка

      if(smth) {
   constraintToolbar.loadLayoutDescription(R.xml.layout_description_1)
} else {
   constraintToolbar.loadLayoutDescription(R.xml.layout_description_2)
}

вызывать motionLayout.requestlayout() чтобы новый MotionScene работал.

Примечание: первый <Transitoin ... /> в <MotionScene ... /> будет работать.

Для меня это работает

          doOnPreDraw {
        motionLayout.loadLayoutDescription(R.xml.scene)
    }
Другие вопросы по тегам