代码之家  ›  专栏  ›  技术社区  ›  Shamsul Arafin Mahtab

单选按钮在循环视图滚动时自动选中,无需单击

  •  1
  • Shamsul Arafin Mahtab  · 技术社区  · 6 年前

    我正在开发一个模型测试应用程序,每个测试有20个问题,每个问题有4个单选按钮选项内的一个电台组。

    现在在滚动和选择单选按钮时,我遇到了自动单选按钮检查。

    我想知道为什么会出现这个问题和解决办法。 这是我的RecylerView示例代码。 `

    @Override
    public void onBindViewHolder(@NonNull QuizQuestionViewHolder holder, int position) {
    
        final QuestionsWithOption questionsWithOption = questionsWithOptionList.get(position);
        final List<Option> optionList = questionsWithOption.getOptions();
    
        holder.quizQuestionTextView.setText(questionsWithOption.getTitle());
    
        if (holder.quizOptionsRadioGroup.getChildCount() == 0) {
            addRadioButtons(holder.quizOptionsRadioGroup, optionList,
                    questionsWithOption.getId());
        }
    }
    
    private void addRadioButtons(RadioGroup quizOptionsRadioGroup,
                                 List<Option> optionList, final int questionId) {
    
        final RadioButton[] radioButtons = new RadioButton[optionList.size()];
    
        for(int i = 0; i < optionList.size(); i++){
            radioButtons[i] = new RadioButton(context);
            radioButtons[i].setText(optionList.get(i).getName());
            radioButtons[i].setTextSize(14);
            radioButtons[i].setId(optionList.get(i).getId());
            radioButtons[i].setTag(optionList.get(i));
            radioButtons[i].setPadding(8, 8, 8, 8);
    
            quizOptionsRadioGroup.addView(radioButtons[i]);
        }
    
        quizOptionsRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton checkedRadioButton = group.findViewById(checkedId);
    
                Option quizOptions = (Option) checkedRadioButton.getTag();
    
                Log.d("Checked", quizOptions.getName());
            }
        });
    }
    

    这是我的应用程序截图。滚动后,这些单选按钮会自动进行检查。我没有按任何按钮。

    ` This is the screenshot of my app. These radio buttons checks automatically after scrolling. I have not touched any button.

    2 回复  |  直到 6 年前
        1
  •  0
  •   Abhijet    6 年前

    解决方法是始终检查必要的条件,以便在onBindView函数中检查和取消选中单选按钮

        2
  •  0
  •   Mohit Patel    6 年前

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public int getItemViewType(int position) {
        return position;
    }
    
    推荐文章