CheckBox不切换点击RecycleView Android

时间:2015-09-21 18:29:21

标签: android android-recyclerview android-checkbox recycler-adapter

我创建了一个回收视图,其中包含每行中都有一个复选框的记录列表。对应于每个checkBox,记录列表中有一个标志。我已经为它实现了一个Adapter和一个自定义监听器。但点击价值并没有改变并在屏幕上反映出来。

public class RecycleStudentsAdapter
        extends RecyclerView.Adapter<RecycleStudentsAdapter.ViewHolder> {

    private final TypedValue mTypedValue = new TypedValue();
    private int mBackground;
    private List<Student> mValues;
    Student template;


    public Student getValueAt(int position) {
        return mValues.get(position);
    }

    public RecycleStudentsAdapter(Context context, List<Student> items) {
        context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
        mBackground = mTypedValue.resourceId;
        mValues = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_student, parent, false);
        view.setBackgroundResource(mBackground);

        return new ViewHolder(view);
    }

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

        template = null;
        template = (Student) mValues.get(position);


        holder.studentId.setText(template.getStudent_disp_id());
        holder.studentName.setText(template.getStudent_name());



        holder.studentCheckBox.setChecked(template.isCheckFlag());
        holder.studentCheckBox.setTag(position);

        holder.studentCheckBox.setOnClickListener(new   CheckBoxListener(position));
        holder.studentCheckBox.setClickable(true);
    }

    public void updateData(List<Student> studentList){

        mValues  = studentList;
        notifyDataSetChanged();
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder {

        public final View mView;
        public final TextView studentId;
        public final TextView studentName;
        public final CheckBox studentCheckBox;

        public ViewHolder(View view) {
            super(view);
            mView = view;

            studentId = (TextView) view.findViewById(R.id.studentId);
            studentName = (TextView) view.findViewById(R.id.studentName);
            studentCheckBox = (CheckBox) view.findViewById(R.id.check);

        }
    }


    class CheckBoxListener implements View.OnClickListener {

        int position;
        CheckBoxListener(int position) {
          this.position = position;
        }

        public void onClick(View v) {

            boolean isChecked = ((CheckBox) v).isChecked();

           ((CheckBox) v).setChecked(!isChecked);


            if (((CheckBox) v).isChecked()) {
                studentList.get(position).setCheckFlag(true);
            } else {
                studentList.get(position).setCheckFlag(false);
            }

            notifyDataSetChanged();
        }


    }
}

1 个答案:

答案 0 :(得分:0)

而不是holder.studentCheckBox.setOnClickListener(); write holder.studentCheckBox.setOnCheckedChangeListener();并使自定义侦听器扩展view.oncheckchangelistener。 删除holder.studentCheckBox.setClickable(true);不需要的行。