当前位置: 首页 > 知识库问答 >
问题:

滚动时自动选中/取消选中复选框的RecycerView

涂溪叠
2023-03-14

这是我的提问者:

 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
}
}
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);
    }
@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();
}
}

这是提问者:

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
    }
}

共有1个答案

潘高洁
2023-03-14

这是因为您的查看器在滚动时被回收。因此,您需要保持每个项位置的复选框的状态。您可以使用SparseBooleanArray来处理这个问题。

您可以这样做:

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) { }
  }

  ...

}
 类似资料:
  • 我正在尝试一个Woocommerce票证插件。默认情况下,该插件会显示一个表,其中包含多个票证变体作为产品,并在下面显示一个文本字段作为其数量和一个提交按钮,以将产品添加到购物车中。 我想用单选按钮或复选框替换这个文本字段,这样用户就可以选择一种类型的票证,然后点击提交按钮将其中的一部分添加到购物车中。 我的想法是在foreach循环中创建一个复选框或单选按钮,该复选框或单选按钮的值为quanti

  • 如何在java Android中创建一个自动选中的复选框?

  • 下载整个项目。zip在这里 此问题的代码在Views文件夹中的CreateSchedule.aspx文件中。 以下是GridView的ASP.NET: 在取消选中某个框后调试事件时,网页上的复选框显示为未选中,但的值为true,即使该复选框在网页上显示为未选中。(chkRow是未选中的复选框) 更新3 我取得了一些突破。我发现复选框的问题在于嵌套的gridview位于使用JavaScript折叠和

  • 我有一个TableView,当我向下滚动表格时,复选框被取消选中。包含复选框的表格列是列,定义为:

  • 我创建了一个windows窗体(到目前为止)只包含复选框。构造函数接受一个参数:。对于这个数组中的每个字符串,我都会创建一个复选框。 例如: null 那么,当另一个复选框调用uncheck时,是否有一种方法可以告诉我的复选框不要运行? 下面是我的代码(它都是手工编写的,所以没有使用visual studio设计器):

  • 我有一个复选框,其中有一对项目,当我单击该复选框时,该项目将添加到名为currentDevice的状态,但当我取消选中该项目时,它将保留“添加项目”,而不是删除它。 当我取消选中该框时,如何从状态中删除项。Im使用“反应本机元素”复选框。谢谢你 代码:

  • 我试图创建一个表单,该表单顶部有一个复选框,当用户选中该复选框时,它会选择其他特定的复选框,但不是所有复选框。我很难通过反复试验或搜索找到答案。我唯一能找到的就是“全选”选项。不是我想要的。 理想情况下,当用户选中“管理包”旁边的复选框时,我想要“Chrome外观组”和“远程启动” 下面是代码和我在这方面的基本尝试,但它不起作用。提前谢谢。 超文本标记语言: Javascript 我不知道这个Ja

  • 背景: 我第一次在项目中使用VueJS,所以我可能没有正确使用它。 我有一个父组件和一个子组件: 父级:ResearchProducts.vue:显示可以按类别筛选的产品列表。筛选器是与产品类别相关的复选框。 子项 : CategoryDisplay.vue :这是处理类别行的视图和方法的组件。 我的问题: 当我单击子组件中的类别复选框时,如果该复选框被选中,该类别将被添加到父组件的过滤器列表中。