`onBindViewHolder` 重复显示结果。

huangapple 未分类评论49阅读模式
英文:

onBindViewHolder repeats the results

问题

我创建了一个显示项目列表并在每个项目旁边有复选框的RecyclerView Adapter

在适配器内部,我创建了:

interface OnItemCheckListener {
    void onItemCheck(DiscoverBooks books);
    void onItemUncheck(DiscoverBooks books);
}

@NonNull
private OnItemCheckListener onItemCheckListener;

@Override
public void onBindViewHolder(BorrowViewHolder holder, int position) {
    holder.bind(items.get(position), position);

    final Discoveritems currentItem = items.get(position);

    holder.setOnClickListener( v -> {
        holder.Cb_Borrow.setChecked( !holder.Cb_Borrow.isChecked());
        if (holder.Cb_Borrow.isChecked()) {
            onItemCheckListener.onItemCheck(currentItem);
        } else {
            onItemCheckListener.onItemUncheck(currentItem);
        }
    } );

}

我为用户提供了选中复选框并稍后使用这些数据的选项。

然而,我发现例如,如果我点击第一个项目的复选框,它会使每7个项目被选中。

例如,我得到:

  • 项目1 - 项目被选中
  • 项目2
  • 项目3
  • 项目4
  • 项目5
  • 项目6
  • 项目7
  • 项目8 - 项目被选中
  • 项目9
  • 项目10
  • 项目11
  • 项目12
  • 项目13
  • 项目14
  • 项目15 - 项目被选中

任何想法为什么会发生这种情况?

谢谢

英文:

I created a RecyclerView Adapter that shows a list of items and next to each on of them there is a Checkbox.

Inside the adapter I created:

interface OnItemCheckListener {
    void onItemCheck(DiscoverBooks books);
    void onItemUncheck(DiscoverBooks books);
}

@NonNull
private OnItemCheckListener onItemCheckListener;


@Override
public void onBindViewHolder(BorrowViewHolder holder, int position) {
    holder.bind(items.get(position), position);

    final Discoveritems currentItem = items.get(position);

    holder.setOnClickListener( v -> {
        holder.Cb_Borrow.setChecked( !holder.Cb_Borrow.isChecked());
        if (holder.Cb_Borrow.isChecked()) {
            onItemCheckListener.onItemCheck(currentItem);
        } else {
            onItemCheckListener.onItemUncheck(currentItem);
        }
    } );

}

Where I give the option for the user to check the Checkbox and to use this data later.

`onBindViewHolder` 重复显示结果。

However, I found that if for example, I clicked the Checkbox of the first item, it makes every 7 item to be checked.

For example what I get:

item 1 - item checked
item 2
item 3
item 4
item 5
item 6
item 7 
item 8 - item checked
item 9
item 10
item 11
item 12
item 13
item 14 
item 15 - item checked

Any idea why it happens?

Thank you

huangapple
  • 本文由 发表于 2020年7月27日 05:37:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/63105973.html
匿名

发表评论

匿名网友

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

确定