Checkbox在RecyclerView问题中启用EditText

时间:2015-06-13 07:57:50

标签: android-edittext android-recyclerview

我正在为测验应用项目工作。我有一个RecyclerView,每个都包含TextView,两个Checkbox和EditText。我想要的是当我检查第二个复选框时,它将启用EditText。我已经完成了这项工作。但问题是如果我检查复选框并在第一行的EditText中键入内容,当我向下滚动其他一些镜像EditText状态时,即使我没有选中Checkbox以启用该行的EditText。

这是我的适配器代码:

    public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentViewHolder> {

    private LayoutInflater inflator;
    private int status;
    Typeface typeface;

    List<Student> students = Collections.emptyList();

    public StudentAdapter(Context context, List<Student> students, String Font) {

        inflator = LayoutInflater.from(context);
        this.students = students;
        typeface = Typeface.createFromAsset(context.getAssets(), Font);

    }


    @Override
    public StudentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflator.inflate(R.layout.student_row, parent, false);

        StudentViewHolder studentViewHolder = new StudentViewHolder(view);


        return studentViewHolder;
    }

    @Override
    public void onBindViewHolder(final StudentViewHolder holder, final int position) {

        final Student currentStudent = students.get(position);
        holder.studentName.setText(currentStudent.student);
        holder.chbIllegal.setTag(students.get(position));
        holder.editText.setTag(students.get(position));

        holder.chbIllegal.setChecked(students.get(position).isSelected());


        holder.chbIllegal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                CheckBox cb = (CheckBox) v;

                final Student contact = (Student) cb.getTag();

                contact.setSelected(cb.isChecked());
                students.get(position).setSelected(cb.isChecked());

              // this will enable editText if checkbox is checked
                if (cb.isChecked()) {
                    holder.editText.setEnabled(true);

                    holder.editText.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {


                            String Illegal = holder.editText.getText().toString();
                            contact.setAgaints_rule(Illegal);
                            Log.d("GExam", currentStudent.student + " " + Illegal);


                        }
                    });

                    holder.chbIllegal.setOnClickListener(null);
                    holder.chbIllegal.setTag(students.get(position));


                } else {

                    holder.editText.setEnabled(false);
                }


            }
        });


        if (currentStudent.getStatus() == 1) {


            holder.chbPresent.setChecked(true);


        } else if (currentStudent.getStatus() == 0) {

            holder.chbPresent.setChecked(false);

        }


    }

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


    class StudentViewHolder extends RecyclerView.ViewHolder {


        TextView studentName;
        CheckBox chbPresent, chbIllegal;
        EditText editText;

        public StudentViewHolder(View itemView) {

            super(itemView);

            studentName = (TextView) itemView.findViewById(R.id.StudentlistText);

            this.studentName.setTypeface(typeface);

            editText = (EditText) itemView.findViewById(R.id.edtAgaintsRule);
            chbPresent = (CheckBox) itemView.findViewById(R.id.chbPresent);

            // chbIllegal will handle the editText
            chbIllegal = (CheckBox) itemView.findViewById(R.id.chbIllegal);

            editText.setEnabled(false);


        }
    }

    public List<Student> getStudentList() {

        return students;

    }
}

如何才能启用选中复选框的行的EditText?

1 个答案:

答案 0 :(得分:0)

最后,我的老师帮助解决了我的问题。我改变的是我将侦听器从onBindViewHolder移动到我的ViewHolder类,如下所示:

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentViewHolder> {

    private LayoutInflater inflator;
    private int status;
    Typeface typeface;

    List<Student> students = Collections.emptyList();

    public StudentAdapter(Context context, List<Student> students, String Font) {

        inflator = LayoutInflater.from(context);
        this.students = students;
        typeface = Typeface.createFromAsset(context.getAssets(), Font);

    }


    @Override
    public StudentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflator.inflate(R.layout.student_row, parent, false);

        StudentViewHolder studentViewHolder = new StudentViewHolder(view);


        return studentViewHolder;
    }

    @Override
    public void onBindViewHolder(final StudentViewHolder holder, final int position) {





        final Student currentStudent = students.get(position);
        holder.studentName.setText(currentStudent.student);
        holder.chbIllegal.setTag(students.get(position));
        holder.editText.setTag(students.get(position));


holder.editText.setText(currentStudent.getAgaints_rule());

        if (currentStudent.isSelected()) {
            holder.editText.setEnabled(true);
        }else{
            holder.editText.setEnabled(false);
        }
        holder.chbIllegal.setChecked(students.get(position).isSelected());


        if (currentStudent.getStatus() == 1) {


            holder.chbPresent.setChecked(true);



        } else if (currentStudent.getStatus() == 0) {

            holder.chbPresent.setChecked(false);

        }


    }

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


    class StudentViewHolder extends RecyclerView.ViewHolder {


        TextView studentName;
        CheckBox chbPresent, chbIllegal;
        EditText editText;

        public StudentViewHolder(View itemView) {

            super(itemView);

            studentName = (TextView) itemView.findViewById(R.id.StudentlistText);

            this.studentName.setTypeface(typeface);

            editText = (EditText) itemView.findViewById(R.id.edtAgaintsRule);
            chbPresent = (CheckBox) itemView.findViewById(R.id.chbPresent);
            chbIllegal = (CheckBox) itemView.findViewById(R.id.chbIllegal);

            chbIllegal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    CheckBox cb = (CheckBox) v;
                    Student s = (Student) v.getTag();
                    Log.e("GExam", String.valueOf(cb.isChecked()));
                    s.setSelected(cb.isChecked());
                    StudentViewHolder.this.editText.setEnabled(cb.isChecked());

                }
            });

            editText.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {

                            Student student = (Student) StudentViewHolder.this.editText.getTag();

                           student.setAgaints_rule(s.toString());
                        }
                    });



        }
    }

    public List<Student> getStudentList() {

        return students;

    }
}