在RecyclerView中选中复选框

时间:2017-10-26 09:43:56

标签: android checkbox android-recyclerview recycler-adapter

以下是我的代码。

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

        }
    });

问题是当我选择包含checkBox checkBox checkBox RecyclerView的{​​{1}}时,我会选中adapter。但是当我签入checkBox时,项目已正确添加,但checkBox选项正在重复。

例如:如果我检查了第一项checkBox并向下滚动第16项checkBox也会被检查。取消选中$(".cross").hide(); $(".mobilemenu").hide(); $(".hamburger").click(function() { $(".mobilemenu").slideToggle("slow", function() { $(".hamburger").hide(); $(".cross").show(); }); }); $(".cross").click(function() { $(".mobilemenu").slideToggle("slow", function() { $(".cross").hide(); $(".hamburger").show(); }); }); $(".clickham").click(function() { $(".mobilemenu").slideToggle("slow", function() { $(".cross").hide(); $(".hamburger").show(); }); });也会取消选中第一个项目。

4 个答案:

答案 0 :(得分:3)

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

private final SparseBooleanArray array=new SparseBooleanArray();

然后在最终视图中添加clickListener并onClick存储所点击项目的位置。

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内部,

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

答案 1 :(得分:3)

public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {
    private List<Student> stList;
    public CardViewDataAdapter(List<Student> students) {
        this.stList = students;
    }

@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  // create a new view
    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
    R.layout.cardview_row, null);

  // create ViewHolder

  ViewHolder viewHolder = new ViewHolder(itemLayoutView);

  return viewHolder;
 }

 @Override
 public void onBindViewHolder(ViewHolder viewHolder, int position) {

  final int pos = position;

  viewHolder.tvName.setText(stList.get(position).getName());

  viewHolder.tvEmailId.setText(stList.get(position).getEmailId());

  viewHolder.chkSelected.setChecked(stList.get(position).isSelected());

  viewHolder.chkSelected.setTag(stList.get(position));


  viewHolder.chkSelected.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    CheckBox cb = (CheckBox) v;
    Student contact = (Student) cb.getTag();

    contact.setSelected(cb.isChecked());
    stList.get(pos).setSelected(cb.isChecked());

    Toast.makeText(
      v.getContext(),
      "Clicked on Checkbox: " + cb.getText() + " is "
        + cb.isChecked(), Toast.LENGTH_LONG).show();
   }
  });

 }

 // Return the size arraylist
 @Override
 public int getItemCount() {
  return stList.size();
 }

 public static class ViewHolder extends RecyclerView.ViewHolder {

  public TextView tvName;
  public TextView tvEmailId;

  public CheckBox chkSelected;

  public Student singlestudent;

  public ViewHolder(View itemLayoutView) {
   super(itemLayoutView);

   tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);

   tvEmailId = (TextView) itemLayoutView.findViewById(R.id.tvEmailId);
   chkSelected = (CheckBox) itemLayoutView.findViewById(R.id.chkSelected);

  }

 }

 // method to access in activity after updating selection
 public List<Student> getStudentist() {
  return stList;
 }
}

有关选中复选框的更多信息,请按此link

答案 2 :(得分:3)

您可能错过了recylerview的一些概念。事实上,recyclerview每9个项目后绑定/回收相同的视图。因此,为了避免这种情况,只需在您的活动中使用setItemViewCacheSize()

示例:

contactListAdapter = new ContactsListAdapter(ContactActivity.this, contactArrayList);
        mRecyclerView.setItemViewCacheSize(contactArrayList.size());
        mRecyclerView.setAdapter(contactListAdapter);
  

public void setItemViewCacheSize(int size)设置屏幕外的数量   在将它们添加到可能共享的回收之前保留的视图   查看池。屏幕外视图缓存始终了解更改   附加适配器,允许LayoutManager重用这些视图   未经修改而无需返回适配器重新绑定它们。   参数:size - 返回前在屏幕外缓存的视图数   他们到一般的回收视图池

答案 3 :(得分:0)

public class SuggestionListAdapter extends RecyclerView.Adapter<SuggestionListAdapter.SuggestionListViewHolder> {

private List<Suggestion> mSuggestionList;
private Context mContext;



public class SuggestionListViewHolder extends RecyclerView.ViewHolder{
    private CheckBox mCheckBox;
    private LinearLayout mParent;


    public SuggestionListViewHolder(View itemView) {
        super(itemView);
        mCheckBox = (CheckBox)itemView.findViewById(R.id.list_display_checkbox);
        mParent =(LinearLayout)itemView.findViewById(R.id.list_parentll);
    }
}

public SuggestionListAdapter(List<Suggestion> suggestionList, Context context) {
    this.mSuggestionList = suggestionList;
    this.mContext = context;
}

@Override
public SuggestionListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View new_suggestion_list_view = LayoutInflater.from(parent.getContext()).inflate(R.layout.suggestion_list,parent,false);
    return new SuggestionListViewHolder(new_suggestion_list_view);
}
@Override
public void onBindViewHolder(final SuggestionListViewHolder holder, final int position) {

    final Suggestion suggestion = mSuggestionList.get(position);

    holder.mCheckBox.setText(suggestion.getCategory());
    holder.mCheckBox.setOnCheckedChangeListener(null);
    holder.mCheckBox.setSelected(suggestion.isSelected());
    holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                suggestion.setSelected(true);
            }else {
                suggestion.setSelected(false);
            }
        }
    });
    holder.mCheckBox.setChecked(suggestion.isSelected());
}

@Override
public int getItemCount() {
    return mSuggestionList.size();
}

}

看看这个例子我相信你会发现你在哪里做错了,你可以解决你的问题。