根据回收器视图中的复选框选择更改按钮状态

时间:2017-02-17 08:56:05

标签: 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);
}
}

0 个答案:

没有答案