Android RecyclerView复选框随机检查 [英] Android RecyclerView Checkbox randomly checks

查看:179
本文介绍了Android RecyclerView复选框随机检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个recyclerview加载卡片复选框。这是适配器。它是精细的,当它加载。但是当我检查一个项目recyclelyview故意,一个复选框被随机选中。如何摆脱这个问题?这是我的适配器。感谢提前。

I have a recyclerview which loads checkboxes on cardviews. This is the adapter. It is fine when it loads. But when I check an item on recyclerview intentionally, a checkbox is checked somewhere random. How can I get rid of this problem? Here is my adapter. Thanks in advance.

public class QuestionsAdapter extends RecyclerView.Adapter<QuestionsAdapter.QuestionsViewHolder> {
List<QuestionModel> Questions;
public Context context;

public QuestionsAdapter(Context context, List<QuestionModel> 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:
        }
    }

}

$ b b

}

}

推荐答案

您的onBindViewHolder()方法中有一个问题...
RecyclerView在滚动期间要重用ViewHolders。因此,您应该为ViewHolder中的所有视图设置状态,以查看一致的数据。现在您只为txtQuestionText设置文本。在这种情况下,您将看到RadioButton的列表项和随机状态的一致文本。

There is a problem in your onBindViewHolder() method... RecyclerView going to reuse ViewHolders during scrolling. For that reason you should set state for all views in the ViewHolder to see consistent data. Now you set only text for "txtQuestionText". In this case you will see consistent text for the list item and random state for the RadioButtons.

简单来说,你应该有这样的:

In brief you should have something like this:

@Override
public void onBindViewHolder(QuestionsViewHolder holder, final int position) {
    holder.setModel(Questions.get(position));
}

...

public class QuestionsViewHolder ...
...
public void setModel(QuestionModel model){
    rbLike.setChecked(model.like);
    rbdisLike.setChecked(model.dislike);
    rbnoComment.setChecked(model.comment);
    txtQuestionText.setText(model.text);
}

这篇关于Android RecyclerView复选框随机检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆