RecyclerView中复选框选择中的重复项 [英] Duplication in Checkbox selection in RecyclerView

查看:165
本文介绍了RecyclerView中复选框选择中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码.

holder.followDiseaseCheckBox.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onClick(View view) {

            if (holder.followDiseaseCheckBox.isChecked()) {

                holder.followDiseaseCheckBox.setChecked(true);

                checkBoxClicked++;
                holder.followDiseaseCheckBox.setChecked(true);
                // for Follow.
                if (isFollowOrUnFollow.equals("FOLLOW")) {

                    ((FollowActivity) context).diseaseListFromAdapter.add(String.valueOf(diseaseMap.get("id")));
                    ((FollowActivity) context).setFollowButton(true);

                }
                // for Unfollow.
                else if (isFollowOrUnFollow.equals("UN_FOLLOW")) {

                    ((FollowTwoActivity) context).unFollowedDiseaseListFromAdapter.add(String.valueOf(diseaseMap.get("id")));
                    ((FollowTwoActivity) context).setUnFollowDiseaseButton(true);
                }


            } else {

                holder.followDiseaseCheckBox.setChecked(false);

                checkBoxClicked--;
                holder.followDiseaseCheckBox.setChecked(false);
                // for Follow.
                if (isFollowOrUnFollow.equals("FOLLOW")) {
                    ((FollowActivity) context).diseaseListFromAdapter.remove(String.valueOf(diseaseMap.get("id")));
                }
                // for Unfollow.
                else if (isFollowOrUnFollow.equals("UN_FOLLOW")) {
                    ((FollowTwoActivity) context).unFollowedDiseaseListFromAdapter.remove(String.valueOf(diseaseMap.get("id")));
                }

                if (checkBoxClicked == 0) {

                    // for Follow.
                    if (isFollowOrUnFollow.equals("FOLLOW")) {
                        ((FollowActivity) context).setFollowButton(false);
                        ((FollowActivity) context).diseaseListFromAdapter.clear();
                    }
                    // for Unfollow.
                    else if (isFollowOrUnFollow.equals("UN_FOLLOW")) {

                        ((FollowTwoActivity) context).setUnFollowDiseaseButton(false);
                        ((FollowTwoActivity) context).unFollowedDiseaseListFromAdapter.clear();
                    }
                }
            }

        }
    });

问题是当我选择包括checkBoxcheckBox时,选中了RecyclerView中的其他checkBox的情况.但是,当我签入adapter项时,正确添加了项,但checkBox选择项却重复了.

Problem is When I select a checkBox including that checkBox some other checkBox in the RecyclerView gets checked. But when I check in adapter item got added properly but checkBox selection are getting duplicated.

例如:如果我检查了第一个项目checkBox并向下滚动了第16个项目checkBox,也会进行检查.取消选中checkBox也会取消选中第一项.

Ex: If I checked first item checkBox and scroll down 16th items checkBox will also be checked. Unchecking that checkBox will uncheck the first item as well.

推荐答案

回收器视图回收OnBindViewHolder中的视图. 因此,当单击项目时,它会反映在其他一些位置. 创建一个全局SparseBooleanArray来存储单击位置.

The recycler view recycles the view in OnBindViewHolder. So when items are clicked it gets reflected in some other positions. To create a global SparseBooleanArray to store the clicked position.

private final SparseBooleanArray array=new SparseBooleanArray();

然后在最终的查看器内部添加clickListener和onClick来存储被单击项的位置.

Then inside final viewholder add the clickListener and onClick store the position of the clicked item.

public class ViewHolder extends RecyclerView.ViewHolder {
    public YOURVIEW view;
    public ViewHolder(View v) {
        super(v);
        view = (YOURVIEW) v.findViewById(R.id.YOURVIEWID);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                array.put(getAdapterPosition(),true);
                notifyDataSetChanged();
            }
        });
    }
}

在OnBindViewHolder内部,

And in inside OnBindViewHolder,

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    if(array.get(position)){
        holder.followDiseaseCheckBox.setChecked(true);
    }else{
        holder.followDiseaseCheckBox.setChecked(false);
    }
}

这篇关于RecyclerView中复选框选择中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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