0

我想要这个:我正在构建几个屏幕,在那里我会遇到类似的问题

1.The capital of India is
a. Delhi
b. Bangalore
c. Chennai

2. Are you sure?
a. Yes
b. No

现在我想要这个在一个屏幕上我想要问题 1,在滑动(水平滚动)时我应该看到问题 2。选项必须是单选按钮。作为一个 Android 菜鸟,我知道我必须使用水平滚动,但我可能在一个 sqlite 文件中有 100 个这样的问题:我如何动态地做到这一点?一旦我调用了活动,就必须从文件中读取 100 个问题,并且我应该有 100 个这样的可滚动屏幕。帮助,我在 android 开发网站或这里找不到太多关于此的内容。

4

2 回答 2

1

如前所述,最好的选择是聆听滑动手势并替换视图中的数据。即您不需要为每个问题创建多个视图。这是我会做的:创建一个xml或多或少像的视图(请注意我跳过了包括layoutheight和layoutwidth之类的mandetory属性,你需要在xml中使用它们)

<LinearLayout
  android:orientation="vertical">
   <TextView
      android:id="+@id/questiontext"/>
   <RadioGroup
      android:id="+@/answersgroup"/>
</LinearLayout>

现在在活动中:

  1. 实现触摸监听器并编写代码来检测滑动。(ali.chousein 的上述答案包含完美的链接,以获得有关如何做的良好参考)。
  2. 在初始加载时将第一个问题设置为视图:

    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);
    }
    
  3. 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.

于 2011-12-30T16:07:12.940 回答
0

您为什么不检测滑动手势并更改活动内容,而不是水平视图?您可以找到许多关于检测滑动手势的指针。其中之一是:在网格布局上进行手势检测

于 2011-12-30T14:23:00.257 回答