Animate LayoutChanges не работает должным образом - PullRequest
0 голосов
/ 05 мая 2018

Я хочу анимировать текст в моем текстовом переключателе. Я просмотрел все вопросы по стеку и документацию, но они не решили мою проблему.

Вот мой XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/tv__inc_pre_sing__screen_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#fff"
        android:gravity="center"
        android:text="PRACTICE"
        android:textAllCaps="true"
        android:textColor="@color/colorAccent"
        android:textSize="16dp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/rl__inc_pre_sing__tm_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:background="#efff"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv__inc_pre_sing__screen_title">

        <ImageView
            android:id="@+id/tv__inc_pre_sing__quotation_mark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:src="@drawable/ic_archive_black_24dp"
            app:layout_constraintTop_toTopOf="parent" />

        <TextSwitcher
            android:id="@+id/tv__inc_pre_sing__teacher_message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/tv__inc_pre_sing__quotation_mark"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:src="@drawable/ic_archive_black_24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv__inc_pre_sing__teacher_message" />

    </android.support.constraint.ConstraintLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Click Me"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <View
        android:layout_width="0dp"
        android:layout_height="16dp"
        android:background="@drawable/ic_launcher_background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/rl__inc_pre_sing__tm_container" />


</android.support.constraint.ConstraintLayout>

Вот мой код

public class Main3Activity extends AppCompatActivity {

    private TextSwitcher mSwitcher;
    Button btnNext;
    int currentIndex=-1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        Resources resources = getApplicationContext().getResources();
        final String[] textString = resources.getStringArray(R.array.teacher_messages);

        btnNext = findViewById(R.id.button);
        mSwitcher = findViewById(R.id.tv__inc_pre_sing__teacher_message);


        mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {

            public View makeView() {
                // TODO Auto-generated method stub
                // create new textView and set the properties like clolr, size etc
                TextView myText = new TextView(Main3Activity.this);
                myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                myText.setTextColor(Color.BLUE);
                return myText;
            }
        });


        // Declare the in and out animations and initialize them
        Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
        Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);

        // set the animation type of textSwitcher
        mSwitcher.setInAnimation(in);
        mSwitcher.setOutAnimation(out);

        // ClickListener for NEXT button
        // When clicked on Button TextSwitcher will switch between texts
        // The current Text will go OUT and next text will come in with specified animation
        btnNext.setOnClickListener(new View.OnClickListener() {
            //
            public void onClick(View v) {
                // TODO Auto-generated method stub
                currentIndex++;
                // If index reaches maximum reset it
                if(currentIndex==textString.length)
                    currentIndex=0;
                mSwitcher.setText(textString[currentIndex]);

                ((ViewGroup) findViewById(R.id.rl__inc_pre_sing__tm_container)).getLayoutTransition()
                        .enableTransitionType(LayoutTransition.CHANGING);

            }
        });

    }
}

Анимация в ConstraintLayout с идентификатором rl__inc_pre_sing__tm_container не работает должным образом. Предположим, что мой TextSwitcher имеет текст, длина которого больше 3, а затем TextSwitcher показывает текст, длина которого равна 1, тогда ConstraintLayout не анимируется, если следующий текст имеет длину 1, то анимация ограничения ограничений анимируется.

Я не могу понять это странное поведение.

1 Ответ

0 голосов
/ 10 мая 2018

Вот проблема: когда TextSwitcher переходит от X строк текста к чему-то меньшему, чем X строк, тогда высота TextSwitcher's не изменяется.

TextSwitcher наследует некоторое поведение от ViewAnimator, который учитывает высоту всех видов при определении высоты виджета. Решение состоит в том, чтобы добавить следующий XML к определению TextSwitcher в файле макета, который будет препятствовать этому поведению:

    android:measureAllChildren="false"

Благодаря этому ответу о переполнении стека , в котором все сказано лучше, чем у меня.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...