Recyclerview中的动态复选框

时间:2018-02-15 07:22:39

标签: android android-recyclerview android-checkbox

在recyclerview中,在某些位置我有多个复选框。在某些位置,我有2个复选框,有些有3个复选框。我无法获得我点击的复选框的ID。当我点击特定复选框时,我得到该特定位置的最后一个复选框的id。

实施例。在位置4处,三个复选框分别具有id 31,32,33。当我点击ID为32的复选框时,

onCheckedChanged(CompoundButton buttonView, boolean isChecked)

返回最后一个复选框的id,即33。

这是代码

 @Override
public void onBindViewHolder(Viewholder holder, final int position) {
    holder.setIsRecyclable(true);

    //String[] columns = new String[]{QID, QUESTION, TYPE, OPTION, CREATED_ON, STATUS, RESPONSE};
    holder.tv_question_no.setText("Question : " + mData.get(position)[0]);
    holder.tv_question.setText(mData.get(position)[1]);

    if (mData.get(position)[2].equals("text")) {
        holder.rb_answer.setVisibility(View.GONE);
        createEditTextview(holder, position);
    } else if (mData.get(position)[2].equals("rating")) {
        holder.rb_answer.setVisibility(View.VISIBLE);
    } else if (mData.get(position)[2].equals("single") || mData.get(position)[2].equals("multi")) {
        holder.rb_answer.setVisibility(View.GONE);
        String[] options = parseOption(mData.get(position)[3]);
        for (int i = 0; i < options.length; i++) {
            checkBox = new CheckBox(mContext);
            createCheckbox(holder, position, options[i], false, Integer.parseInt(position + "" + i));
        }
    }
}


 private void createCheckbox(Viewholder holder, final int position, String text, boolean isChecked, int id) {

    CheckboxModel checkboxModel = new CheckboxModel();
    RelativeLayout.LayoutParams layoutParamss =
            new RelativeLayout.LayoutParams
                    (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParamss.addRule(RelativeLayout.BELOW, id - 1);

    checkBox.setText(text);
    checkBox.setId(id);
    checkBox.setLayoutParams(layoutParamss);
    checkBox.setChecked(isChecked);
    checkboxModel.setView(checkBox);
    checkboxModel.setPosition(position);
    allViewInstances.add(checkboxModel);
    holder.rl_layout.addView(checkBox);
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mData.get(position)[2].equals("multi")) {
                call(checkBox.getId(), isChecked);
            }
        }
    });
}


 private void call(int id, boolean isChecked) {
    for (int i = 0; i < allViewInstances.size(); i++) {
        if (isChecked && allViewInstances.get(i).getView().getId() == id){
            CheckBox chk = (CheckBox) allViewInstances.get(i).getView();
            chk.setChecked(true);
        }else {
            CheckBox chk = (CheckBox) allViewInstances.get(i).getView();
            chk.setChecked(false);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

即使我已经为动态复选框创建了适配器,也可以看看

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;


public class  DiscussionAdapter extends RecyclerView.Adapter<DiscussionAdapter.ViewHolder1> implements View.OnClickListener {

    private ArrayList<PDQuestions> list;
    private Context context;
    private SparseBooleanArray questOptions = new SparseBooleanArray();

    public DiscussionAdapter(Context context, ArrayList<PDQuestions> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public DiscussionAdapter.ViewHolder1 onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.personal_discussion_adapter, parent, false);
        return new ViewHolder1(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder1 holder, int position) {
        holder.llOptions.removeAllViews();
        ArrayList<PDAnswers> optionsList = new ArrayList<>();
        optionsList.addAll(list.get(position).getLstResponseAnswer());
        for (final PDAnswers optionAns : optionsList) {
            CheckBox cBoxOption = new CheckBox(context);
            cBoxOption.setId(optionAns.getAnswerId());
            cBoxOption.setTag(list.get(position).getQuestionId());
            cBoxOption.setText(optionAns.getAnswer());
            holder.llOptions.addView(cBoxOption);
            cBoxOption.setChecked(questOptions.get(optionAns.getAnswerId()));
            cBoxOption.setOnClickListener(this);
        }
        holder.txtQuestion.setText(list.get(position).getQuestions());

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

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

    @Override
    public void onClick(View v) {
        Log.i("Sandy", "inside onClick: ");
        LinearLayout llOptions = (LinearLayout) v.getParent();
        for (int i = 0; i < llOptions.getChildCount(); i++) {
            CheckBox buttonView = (CheckBox) llOptions.getChildAt(i);
            if (v.getId() == buttonView.getId()) {
                questOptions.append(buttonView.getId(), true);
                buttonView.setChecked(true);
            } else {
                questOptions.append(buttonView.getId(), false);
                buttonView.setChecked(false);
            }
        }
    }

    public class ViewHolder1 extends RecyclerView.ViewHolder {

        TextView txtQuestion;
        LinearLayout llOptions;

        public ViewHolder1(final View view) {
            super(view);
            txtQuestion = (TextView) view.findViewById(R.id.txtQuestion);
            llOptions = (LinearLayout) view.findViewById(R.id.llOptions);
        }
    }
}

希望它能帮到你!!

答案 1 :(得分:1)

使用setTag和getTag

在checkBox.setId(id);

之后添加以下行
checkBox.setTag(id);

替换

call(checkBox.getId(), isChecked);

call((int) buttonView.getTag(), isChecked);