滚动时会自动选中/取消选中带有复选框的RecyclerView [英] RecyclerView with checkbox checked/unchecked automatically when I am scrolling

查看:267
本文介绍了滚动时会自动选中/取消选中带有复选框的RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的QuestionHolder:

 private class QuestionHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private JSONObject question;
    private CheckBox checkBox;
    private TextView question_txt;
    public QuestionHolder(final LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.question_item, parent, false));

        checkBox  = (CheckBox) itemView.findViewById(R.id.question_checkbox);

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //try {
                    //Toast.makeText(getActivity(), "ID: " + question.get("_id"), Toast.LENGTH_SHORT).show();
                //} catch (JSONException e) { }
                // TODO: If this was checked then add the question's id to the answered list
            }
        });
        question_txt = (TextView) itemView.findViewById(R.id.question);
        itemView.setOnClickListener(this);
    }

在这里,我绑定questiontxt:

public void bind(JSONObject question) {
  this.question = question;
  try {
    question_txt.setText(question.getString("_question"));
  } catch (JSONException je) { }
}

@Override
public void onClick(View v) {
  // TODO: If this was checked then add the question's id to the answered list
}
}

这是我的适配器,扩展了QuestionHolder:

this is my Adapter which extend QuestionHolder:

private class QuestionAdapter extends RecyclerView.Adapter<QuestionHolder> {

    private JSONArray questions;

    public QuestionAdapter(JSONArray questions) {
        this.questions = questions;
    }

    @Override
    public QuestionHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{
        return new QuestionHolder(LayoutInflater.from(getActivity()), parent);
    }

这是onBindViewHolder:

@Override
public void onBindViewHolder(QuestionHolder holder, int position) {
  try {
    final JSONObject question = questions.getJSONObject(position);
    holder.bind(question);
  } catch (JSONException je) { }
}

@Override
public int getItemCount() {
  return questions.length();
}
}

这是QuestionHolder:

private class QuestionHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private JSONObject question;
    private CheckBox checkBox;
    private TextView question_txt;
    public QuestionHolder(final LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.question_item, parent, false));

        checkBox  = (CheckBox) itemView.findViewById(R.id.question_checkbox);

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //try {
                    //Toast.makeText(getActivity(), "ID: " + question.get("_id"), Toast.LENGTH_SHORT).show();
                //} catch (JSONException e) { }
                // TODO: If this was checked then add the question's id to the answered list
            }
        });
        question_txt = (TextView) itemView.findViewById(R.id.question);
        itemView.setOnClickListener(this);
    }

    public void bind(JSONObject question) {
        this.question = question;
        try {
            question_txt.setText(question.getString("_question"));
        } catch (JSONException je) { }
    }

    @Override
    public void onClick(View v) {
        // TODO: If this was checked then add the question's id to the answered list
    }
}

推荐答案

这是因为滚动时会回收ViewHolder.因此,您需要保留每个项目位置的CheckBox状态.您可以使用 SparseBooleanArray 来处理.

This is because your ViewHolder is recycled when scrolling. So, you need to keep the state of the CheckBox for each item position. You can use SparseBooleanArray to handle this.

您可以执行以下操作:

private class QuestionAdapter extends RecyclerView.Adapter<QuestionHolder> {

  SparseBooleanArray checkedItems = new SparseBooleanArray();

  ...

  @Override
  public void onBindViewHolder(QuestionHolder holder, int position) {
    try {
      final JSONObject question = questions.getJSONObject(position);

      // Remember to change access modifier of checkBox in your ViewHolder
      // Get the state from checkedItems. If no previous value, it will return false.
      holder.checkBox.setChecked(checkedItems.get(position));

      // This is a sample. Do not use create listener here.
      checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          // save the check state
          checkedItems.put(position, true);
        }

      holder.bind(question);
    } catch (JSONException je) { }
  }

  ...

}

这篇关于滚动时会自动选中/取消选中带有复选框的RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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