以编程方式控制回收器视图中的复选框行为

时间:2017-02-16 09:01:54

标签: android android-recyclerview

我在视图中的每张卡片上都有一个带有复选框的回收站视图。当我点击视图中的特定复选框时,我想取消选中所有其他复选框(条件可能像卡包含奇数等)。我怎样才能做到这一点?我的适配器代码如下。

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

ArrayList<Batch> batches;
ArrayList<CourseSlug> courses;
boolean isInstituteStudent;

public void setBatches(ArrayList<Batch> batches) {
    this.batches = batches;
    notifyDataSetChanged();
}

public void setCourses(ArrayList<CourseSlug> courses) {
    this.courses = courses;
    notifyDataSetChanged();
}

public ArrayList<CourseSlug> getCourses() {
    return courses;
}

public ArrayList<Batch> getBatches() {
    return batches;
}

public void setInstituteStudent(boolean instituteStudent) {
    isInstituteStudent = instituteStudent;
}

public boolean isInstituteStudent() {
    return isInstituteStudent;
}

public PlatformAdapter() {
    courses = new ArrayList<>();
    batches = new ArrayList<>();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell_platform_item, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    if (isInstituteStudent) {
        Batch batch = batches.get(position);
        holder.enrolment.setText(batch.getName());
        holder.selectEnrollment.setChecked(batch.isPreselect());
    } else {
        final CourseSlug course = courses.get(position);
        holder.enrolment.setText(course.getName());
        holder.selectEnrollment.setChecked(course.isPreselect());


    }
}

@Override
public int getItemCount() {
    return isInstituteStudent ? batches.size() : courses.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

    TextView enrolment;
    CheckBox selectEnrollment;

    public ViewHolder(final View itemView) {
        super(itemView);
        enrolment = (TextView) itemView.findViewById(R.id.tv_entrollment);
        selectEnrollment = (CheckBox) itemView.findViewById(R.id.cb_select_entrollment);
        selectEnrollment.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                int index = getLayoutPosition();
                if(isInstituteStudent) {
                    Batch batch = batches.get(index);
                    batch.setPreselect(b);
                } else {
                    CourseSlug course = courses.get(index);
                    course.setPreselect(b);



                        }
                    }


        });

    }
}

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

在我的Courseslug类中,有一个名为isTrue()的参数; if,courseSlug.isTrue()== true;然后哪个卡的复选框满足我的条件,它会被检查,其他未选中/选中的复选框将被取消选中。这就是我想要的。

public class CourseSlug {
String name;
String slug;
boolean preselect;
boolean istrue;

public boolean isTrue() {
    return istrue;
}

public void setisTrue(boolean isTrue) {
    this.isTrue = istrue;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSlug() {
    return slug;
}

public void setSlug(String slug) {
    this.slug = slug;
}

public boolean isPreselect() {
    return preselect;
}

public void setPreselect(boolean preselect) {
    this.preselect = preselect;
}}

2 个答案:

答案 0 :(得分:0)

由于视图不一定是可见的,因此需要在适配器中保留一个布尔状态变量。当用户点击&#34; master&#34;时切换变量。复选框。然后在onBindViewHolder()中使用此变量来设置每行中复选框的选中状态。

答案 1 :(得分:0)

我已编辑了您的onBindViewHolder方法。希望这有用:)

 private SparseArray<SparseBooleanArray> sparseArray = new SparseArray<>();

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

                final CourseSlug course = courses.get(position);
                holder.enrolment.setText(course.getName());
                holder.selectEnrollment.setChecked(course.isPreselect());
                holder.selectEnrollment.setId(position);
                holder.selectEnrollment.setChecked(false);

            if (sparseArray.get(holder.selectEnrollment.getId()) != null) {
                boolean isChecked = sparseArray.get(holder.selectEnrollment.getId()).get(holder.selectEnrollment.getId());
                holder.selectEnrollment.setChecked(isChecked);
            }

    holder.selectEnrollment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sparseArray.clear();

            SparseBooleanArray checkedPosition = new SparseBooleanArray();
            checkedPosition.put(v.getId(), true);
            sparseArray.put(tagId, checkedPosition);
            notifySetDataChanged();
                }
            });


    }