前述のように、最良のオプションは、スワイプ ジェスチャを聞いて、ビュー内のデータを置き換えることです。つまり、質問ごとに複数のビューを作成する必要はありません。これが私がやったことです:xmlが多かれ少なかれ似ているビューを作成します(layoutheightやlayoutwidthなどの必須属性を含めることをスキップしたことに注意してください。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);
}
右スワイプまたは左スワイプのジェスチャ検証を実行した後のコード部分
//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);
}
現在、これを行う代替方法がいくつかあります。アダプター、リストビューなどを使用して、すべてのデータ構造クラスに任意に名前を付けましたが、要点を理解していただければ幸いです..同じActivity(Screen)を使用して、ある質問から別の質問への移動を処理するより多くの方法ですあなたを指摘します。