当前位置:  开发笔记 > 编程语言 > 正文

Android RecyclerView Checkbox随机检查

如何解决《AndroidRecyclerViewCheckbox随机检查》经验,为你挑选了0个好方法。

我有一个recyclerview,它在cardviews上加载复选框。这是适配器。加载时很好。但是,当我故意在recyclerview上检查某个项目时,会在某个随机位置选中一个复选框。我如何摆脱这个问题?这是我的适配器。提前致谢。

public class QuestionsAdapter extends RecyclerView.Adapter {
List Questions;
public Context context;

public QuestionsAdapter(Context context, List questions) {
    this.Questions = questions;
    this.context = context;
}

@Override
public QuestionsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.questions_card_view, parent, false);
    return new QuestionsViewHolder(v);
}

@Override
public void onBindViewHolder(QuestionsViewHolder holder, final int position) {
    holder.txtQuestionText.setText(Questions.get(position).QuestionText);

}

@Override
public int getItemCount() {
    return Questions.size();

}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);

}

public class QuestionsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public RadioButton rbLike;
    public RadioButton rbdisLike;
    public RadioButton rbnoComment;
    public TextView txtQuestionText;

    public QuestionsViewHolder(View itemView) {
        super(itemView);
        rbLike = (RadioButton) itemView.findViewById(R.id.like);
        rbdisLike = (RadioButton) itemView.findViewById(R.id.Dislike);
        txtQuestionText = (TextView) itemView.findViewById(R.id.txtCardQuestion);
        rbnoComment = (RadioButton) itemView.findViewById(R.id.NoComment);
        rbnoComment.setOnClickListener(this);
        rbdisLike.setOnClickListener(this);
        rbLike.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.like:
                rbLike.setChecked(true);
                rbnoComment.setChecked(false);
                rbdisLike.setChecked(false);

                break;
            case R.id.Dislike:
                rbdisLike.setChecked(true);
                rbnoComment.setChecked(false);
                rbLike.setChecked(false);
                break;
            case R.id.NoComment:
                rbnoComment.setChecked(true);
                rbLike.setChecked(false);
                rbdisLike.setChecked(false);
                //do something
                break;

            default:
        }
    }

}

}

推荐阅读
李桂平2402851397
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有