如前所述,最好的选择是聆听滑动手势并替换视图中的数据。即您不需要为每个问题创建多个视图。这是我会做的:创建一个xml或多或少像的视图(请注意我跳过了包括layoutheight和layoutwidth之类的mandetory属性,你需要在xml中使用它们)
<LinearLayout
android:orientation="vertical">
<TextView
android:id="+@id/questiontext"/>
<RadioGroup
android:id="+@/answersgroup"/>
</LinearLayout>
现在在活动中:
- 实现触摸监听器并编写代码来检测滑动。(ali.chousein 的上述答案包含完美的链接,以获得有关如何做的良好参考)。
在初始加载时将第一个问题设置为视图:
QandA_CustomDataObject dataItem = questionArr.get(0);
((TextView)findViewById(R.id.questiontext)).setText(dataItem.question);
int answearrsize = dataItem.answers.size();
RadioGroup rg = ((RadioGroup)findViewById(R.id.answersgroup));
for(int i=0;i<answearrsize;i++) //Dynamically create the radio buttons
{
AnswerObj ao = dataItem.get(0).answers.get(i);
RadioButton rb = new RadioButton(this);
rb.setText(ao.text);
rb.setTag(ao.isCorrectAnswer); //A string saying TRUE or FALSE
rg.addView(rb);
}
Now on the code part after you have performed the gesture validation for right swipe or left swipe
//Lets say you ++ or -- a variable named currentQuestionNumber based on the swipe direction)
QandA_CustomDataObject dataItem = questionArr.get(currentQuestionNumber);
((TextView)findViewById(R.id.questiontext)).setText(dataItem.question);
int answearrsize = dataItem.answers.size();
RadioGroup rg = ((RadioGroup)findViewById(R.id.answersgroup));
rg.removeAllViews(); //Clears away the last questions answer options
for(int i=0;i<answearrsize;i++) //Dynamically create the radio buttons
{
AnswerObj ao = dataItem.get(0).answers.get(i);
RadioButton rb = new RadioButton(this);
rb.setText(ao.text);
rb.setTag(ao.isCorrectAnswer); //A string saying TRUE or FALSE
rg.addView(rb);
}
Now there are couple of alternative ways of doing this. Using adapters, list views etc etc.
I arbitararely named all the data structure classes, but I hope you get the point.. its more of a way to handle moving from one question to another using the same Activity(Screen) that I wanted to point you out.