Android CustomAttribute в MotionLayout для изменения текста в TextView - PullRequest
0 голосов
/ 10 ноября 2018

Любой может помочь мне с правильным способом изменить текст TextView в MotionLayout ... это то, что я делаю.

Я тестирую MotionLayout в простом приложении ... Я дошел до части в Обучающих курсах о CustomAttributes

С их помощью вы можете изменить BackgroundColor из View, а также textColor, используя customColorValue

В этом случае вы можете видеть, что это работает очень хорошо, изменяя эти значения в начальной и конечной сцене:

        <CustomAttribute
            motion:attributeName="backgroundColor"
            motion:customColorValue="#004A6D" />

        <CustomAttribute
            motion:attributeName="textColor"
            motion:customColorValue="#000000" />

enter image description here

Также отмечу, что есть customStringValue, поэтому я думаю, что могу изменить текст TextView на «ДО» -> «ПОСЛЕ». Но когда я пытаюсь установить это с помощью CustomAttribute, происходит сбой приложения.

В стартовой сцене:

<CustomAttribute
            motion:attributeName="Text"
            motion:customStringValue="BEFORE" />

И в финальной сцене:

        <CustomAttribute
            motion:attributeName="Text"
            motion:customStringValue="AFTER" />

Outside MotionScene textView текст TEST :

  • Когда я устанавливаю CustomAttribute только для конечной сцены ... текст изменяется с начального значения ТЕСТ до конца ПОСЛЕ значения ... так что это частично работает, но никогда не возвращаться в исходное состояние.
  • Это происходит также, когда на TextView не установлен первоначальный текст. Работает частично.

enter image description here

Итак ... любой может помочь мне с правильным способом изменить текст TextView в MotionLayout.

Ответы [ 2 ]

0 голосов
/ 26 февраля 2019

Я тестирую и так

enter image description here

//constraintSetStart
<ConstraintSet...>
<Constraint
  android:id="@+id/button">
    <CustomAttribute
       motion:attributeName="Text"
       motion:customStringValue="CLOSE"/>
</Constraint>
</ConstraintSet>

//constraintSetEnd
<ConstrainSet..>
<Constraint
   android:id="@+id/button">
     <CustomAttribute
       motion:attributeName="Text"
       motion:customStringValue="OPEN"/>
 </Constraint>
</ConstraintSet>
0 голосов
/ 14 февраля 2019

вы можете сделать это программно, используя TransitionListener, как это:

motionLayout.setTransitionListener(object : MotionLayout.TransitionListener {
    override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {
        // ADD YOUR CODE HERE
    }

    override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
        // ADD YOUR CODE HERE
    }

    override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
        textView.text = if(p3==0f) "before" else "after"
    }

    override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {

    }
})
...