Правильный способ сделать анимацию между представлениями одного и того же типа? - PullRequest
0 голосов
/ 28 декабря 2010

Я занимаюсь обучающим приложением, в котором задается набор вопросов с несколькими вариантами ответов. Я бы хотел сделать анимацию слайдов между двумя вопросами - когда пользователь правильно отвечает на один вопрос, он скользит влево, а новый - справа.

В настоящее время мой макет вопроса / ответа выглядит следующим образом (question.xml):

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center"
        >
                <TextView
                        android:id="@+id/question" 
                    android:layout_width="fill_parent"  
                    android:layout_height="wrap_content" 
                    android:textColor="#000000"
                    android:textSize="8pt"
                    android:textStyle="bold"
                android:layout_margin="5px"
                    android:layout_marginBottom="10dp"
                    android:clickable="true"
                    />
                <RadioGroup android:id="@+id/answers"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_margin="5px"
                        >
                        <RadioButton android:id="@+id/answer_a"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:textColor="#000000"
                                android:textSize="7pt"
                                android:layout_margin="5dp"
                                android:layout_marginBottom="10dp"
                                />
                        <RadioButton android:id="@+id/answer_b"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:textColor="#000000"
                                android:textSize="7pt"
                                android:layout_margin="5dp"
                                android:layout_marginBottom="10dp"
                                />
                        <RadioButton android:id="@+id/answer_c"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:textColor="#000000"
                                android:textSize="7pt"
                                android:layout_margin="5dp"
                                android:layout_marginBottom="10dp"
                                />
            </RadioGroup>
        </LinearLayout>
</ScrollView>

После ответа на вопрос и оценки ответа я просто изменил текст вопроса и три ответа. Довольно просто.

Теперь я попытался использовать ViewFlipper для анимации:

<ViewFlipper android:id="@+id/flipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <include android:id="@+id/current_view" layout="@layout/question" />
    <include android:id="@+id/next_view"    layout="@layout/question" />
</ViewFlipper>

Но таким образом я не могу получить доступ к просмотрам в текущем и следующем вопросе отдельно через findViewById.

Нужно ли для этого иметь два одинаковых макета вопросов только с разными идентификаторами? это кажется излишним для меня. Или есть другой способ получить доступ к только к вопросу и ответам во втором представлении?
Или есть более простой способ сделать это?

Спасибо!

1 Ответ

0 голосов
/ 28 декабря 2010

Хорошо, в конце концов я сделал это - я раздуваю макет questions.xml каждый раз, когда готовлю новый вопрос, добавляю его в флиппер и переворачиваю. И поскольку я не хочу, чтобы флиппер становился огромным, я удаляю предыдущие старые сальто, если их больше одного (в основном всегда, кроме первого раза).

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.question, null);

    txtPracticeQuestion = (TextView)    layout.findViewById(R.id.question);
    rgrpPracticeAnswers = (RadioGroup)  layout.findViewById(R.id.answers);
    rbtnPracticeAnswerA = (RadioButton) layout.findViewById(R.id.answer_a);
    rbtnPracticeAnswerB = (RadioButton) layout.findViewById(R.id.answer_b);
    rbtnPracticeAnswerC = (RadioButton) layout.findViewById(R.id.answer_c);

    rbtnPracticeAnswerA.setOnClickListener(this);
    rbtnPracticeAnswerB.setOnClickListener(this);
    rbtnPracticeAnswerC.setOnClickListener(this);

    txtPracticeQuestion.setText(currentQuestion.get("question").toString());
    rbtnPracticeAnswerA.setText(currentQuestion.get("answer_a").toString());
    rbtnPracticeAnswerB.setText(currentQuestion.get("answer_b").toString());
    rbtnPracticeAnswerC.setText(currentQuestion.get("answer_c").toString());

    if (flipper.getChildCount() > 1)
        flipper.removeViewAt(0);

    flipper.addView(layout);

Это будет правильный подход?

...