在相同类型的视图之间制作动画的正确方法?

发布于 2024-10-09 14:47:54 字数 3498 浏览 3 评论 0原文

我正在做一个训练应用程序,练习一组多项选择题。我想在两个问题之间制作幻灯片动画 - 当用户正确回答一个问题时,它会滑向左侧,而新问题会从右侧滑入。

目前,我的问题/答案布局如下所示(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 分别访问当前问题和下一个问题中的视图。

我是否需要有两个相同的问题布局(只是 ID 不同)才能执行此操作?这对我来说似乎多余。或者是否有其他方法可以访问第二个视图中的问题和答案?
或者有更简单的方法来完成这个任务吗?

谢谢!

I'm doing a training application that practices a set of multiple-choice questions. I'd like to do a slide animation between two questions - when the user correctly answers one question, it slides away to the left while the new one slides in from right.

Currently, my question/answer layout looks like this (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>

After answering the question and evaluating the answer, I just changed the text of the question and the three answers. Pretty straightforward.

Now, I've tried using ViewFlipper to do the animations:

<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>

But that way I can't access the views in the current and next question separately via findViewById.

Do I need to have two same question layouts just with different IDs to do this? it seems redundant to me. Or is there another way to access only the question&answers in the second view?
Or is there an easier way to accomplish this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装迷糊 2024-10-16 14:47:54

好吧,最后我这样做了 - 每次准备一个新问题时,我都会膨胀 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);

这是正确的方法吗?

Ok, in the end I did this - I inflate the questions.xml layout every time I prepare a new question, add it to the flipper and flip it.
And as I don't want the flipper to become huge, I delete the previous old flips if there's more than one (basically always except the first time).

    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);

Would this be the correct approach?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文