Android RecyclerView and CheckableTextView Checking Bug

huangapple go评论67阅读模式
英文:

Android RecyclerView and CheckableTextView Checking Bug

问题

我有一个带有RecyclerView的活动,其中包含带有CheckableTextView的RecyclerView。列表中有大约15个元素,可以滚动查看。我使用RecyclerView适配器来填充列表,写入文本视图并侦听点击事件,我使用点击事件来切换复选框的状态。到目前为止,一切都按预期工作。但是当我勾选其中一个项目并滚动然后再滚动回来时,我看到元素没有被选中,然后其他的一个元素会自行选中。每次我上下滚动时,都会有其他元素被选中。我找不到关于这个问题的任何信息。

英文:

I have an activity with recyclerview that containts RecyclerView with CheckableTextViews. There like 15 elements in the list and it is scrollable. I use RecyclerView Adapter to fill the list, write to the textviews and listen the click event an I use the click event to toggle the checkbox state. So far everything is working as expected. But when I check one of the items and scroll down and back up. I see that element is unchecked, then one of the other elements get checked by itself. Everytime I scroll up and down some other element got checked. I cannot find anything about this issue.

答案1

得分: 0

我现在将复选框状态作为参数发送到ViewHolder,并使用setChecked来更新视图。问题已解决。

class WorkerViewHolder extends RecyclerView.ViewHolder {
    private final CheckedTextView textViewRow;
    WorkerViewHolder(View v) {
        super(v);
        textViewRow = v.findViewById(R.id.textViewRow);
        v.setOnClickListener(v1 -> {
            Personnel p = (Personnel) v.getTag();
            textViewRow.setChecked(!textViewRow.isChecked());
            checklist[workers.indexOf(p)] = textViewRow.isChecked();
        });
    }
    void setLine(Personnel worker, boolean check) {
        textViewRow.getRootView().setTag(worker);
        textViewRow.setText(worker.Nick);
        textViewRow.setChecked(check);
    }
}
英文:

I'm now sending the checkbox state as a parameter to ViewHolder and update the View with setChecked. Problem solved.

class WorkerViewHolder extends RecyclerView.ViewHolder {
    private final CheckedTextView textViewRow;
    WorkerViewHolder(View v) {
        super(v);
        textViewRow = v.findViewById(R.id.textViewRow);
        v.setOnClickListener(v1 -> {
            Personnel p = (Personnel) v.getTag();
            textViewRow.setChecked(!textViewRow.isChecked());
            checklist[workers.indexOf(p)] = textViewRow.isChecked();
        });
    }
    void setLine(Personnel worker, boolean check) {
        textViewRow.getRootView().setTag(worker);
        textViewRow.setText(worker.Nick);
        textViewRow.setChecked(check);
    }
}

huangapple
  • 本文由 发表于 2020年9月25日 18:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64062258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定