英文:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论