Duplication in Checkbox selection in RecyclerView

后端 未结 4 1904
无人共我
无人共我 2020-12-11 23:38

Below is the my code.

holder.followDiseaseCheckBox.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
          


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 00:43

    public class SuggestionListAdapter extends RecyclerView.Adapter {
    
    private List 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 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();
    }
    

    }

    have a look at this example i am sure you will found where you are doing mistake and you can solve your problem.

提交回复
热议问题