我目前正在Android开发课程中为一个项目开发测验应用程序,在使用Fragments(尤其是FragmentStatePagerAdapter)时遇到问题。
该应用程序应该会给您六个问题,并且您应该能够左右滑动以在两个问题之间导航。此功能有效。看起来像这样:
这些问题和答案是使用Android Room从数据库动态创建的。我生成6个唯一的问题,每个问题向FragmentStatePagerAdapter添加一个Fragment。问题是当您实际滑过所有问题时出现的。到达问题3,然后滚动回到问题1,然后问题1如下所示:
我意识到片段被销毁然后恢复了。但是,使用Log.d我发现onCreate仍然调用完全相同的方法,甚至再次检索所有答案。问题是它没有将其分配给单选按钮,我也不知道为什么。片段的onCreate看起来像这样:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.page_1, container, false);
states = rootView.findViewById(R.id.textView5);
radioGroup = rootView.findViewById(R.id.radioGroup);
questionCount = rootView.findViewById(R.id.questionCount);
answerRadios.add(rootView.findViewById(R.id.radioButton));
answerRadios.add(rootView.findViewById(R.id.radioButton2));
answerRadios.add(rootView.findViewById(R.id.radioButton3));
questionCount.setText("Question " + order + "/6");
// Gets the question in this particular order
LiveDataUtil.observeOnce(db.questions().getQuestion(qId, order), (question) -> {
states.setText("What is the capital of " + question.state + "?");
// Get the answers for the question
LiveDataUtil.observeOnce(db.answers().getAnswers(question.id), (answers) -> {
for (int i = 0, len = answers.size(); i < len; i++) {
RadioButton radio = answerRadios.get(i);
QuizAnswer answer = answers.get(i);
radio.setText(answer.answerText);
}
});
radioGroup.setOnCheckedChangeListener
...more code
});
return rootView;
}
片段模板(page_1.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:text="Capital Quiz!"
android:textSize="36sp" />
<TextView
android:id="@+id/questionCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:text="Question 1/6" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="QUESTION"
android:textSize="18sp" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Answer 1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Answer 2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer 3" />
</RadioGroup>
</LinearLayout>
仅供参考,LiveDataUtil如下所示:
public class LiveDataUtil {
public static <T> void observeOnce(LiveData<T> liveData, Observer<T> observer) {
liveData.observeForever(o -> {
liveData.removeObserver(observer);
observer.onChanged(o);
});
}
}