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

根据android中回收器视图中的复选框选择更改按钮状态,android,checkbox,android-recyclerview,Android,Checkbox,Android Recyclerview,我有一个带有适配器类的回收器视图。在单项视图中,还有一个复选框。在我的布局视图中,它包含一个按钮和上面提到的回收器视图。当我选中视图中的任何复选框时,按钮应被激活(将颜色更改为活动)。当未选择任何内容时,视图按钮应以停用的形式显示。我怎么可能?有什么例子吗?代码如下 public class PlatformAdapter extends RecyclerView.Adapter<PlatformAdapter.ViewHolder> { ArrayList<Batch>

我有一个带有适配器类的回收器视图。在单项视图中,还有一个复选框。在我的布局视图中,它包含一个按钮和上面提到的回收器视图。当我选中视图中的任何复选框时,按钮应被激活(将颜色更改为活动)。当未选择任何内容时,视图按钮应以停用的形式显示。我怎么可能?有什么例子吗?代码如下

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);
}
}
公共类PlatformAdapter扩展了RecyclerView.Adapter{
ArrayList批次;
ArrayList课程;
布尔是学生;
公共作废收件批次(ArrayList批次){
this.batches=批次;
notifyDataSetChanged();
}
公共课程(ArrayList课程){
本课程=课程;
notifyDataSetChanged();
}
公共阵列列表getCourses(){
返回课程;
}
公共ArrayList getBatches(){
退货批次;
}
public void setInstituteStudent(布尔instituteStudent){
isInstituteStudent=学院学生;
}
公共布尔值isInstituteStudent(){
返回是机构学生;
}
公共平台适配器(){
courses=newarraylist();
批次=新的ArrayList();
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
View View=LayoutInflater.from(parent.getContext()).flate(R.layout.list\u cell\u platform\u item,parent,false);
返回新的ViewHolder(视图);
}
@凌驾
公共无效onBindViewHolder(最终视图持有人,最终整型位置){
如果(isInstituteStudent){
批次=批次。获取(位置);
holder.registration.setText(batch.getName());
holder.selectEnrollment.setChecked(batch.isPreselect());
}否则{
最终课程推送课程=课程。获取(位置);
holder.registration.setText(course.getName());
holder.selectEnrollment.setChecked(course.isPreselect());
}
}
@凌驾
public int getItemCount(){
返回isInstituteStudent?batches.size():courses.size();
}
类ViewHolder扩展了RecyclerView.ViewHolder{
TextView注册;
复选框选择注册;
公共视图持有者(最终视图项视图){
超级(项目视图);
注册=(TextView)itemView.findViewById(R.id.tv\u entrollment);
selectEnrollment=(复选框)itemView.findViewById(R.id.cb\u select\u entrollment);
选择Enrollment.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮复合按钮,布尔b){
int index=getLayoutPosition();
如果(isInstituteStudent){
Batch=batches.get(索引);
批次设置预选(b);
}否则{
CourseSlug course=courses.get(索引);
课程设置预选(b);
}
}
});
}
}
@凌驾
公共长getItemId(int位置){
返回super.getItemId(位置);
}
}
下面给出了我的Recyclerview和button实现类

public class SelectPlatform extends BaseActivity {

PlatformAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_platform);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    initializeViews(
            findViewById(R.id.ll_content_area),
            findViewById(R.id.tv_error),
            findViewById(R.id.pb_loading)
    );

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_platform);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter = new PlatformAdapter();
    recyclerView.setAdapter(adapter);


private void renderCourses(ArrayList<CourseSlug> courses) {
    getSupportActionBar().setTitle("Select Courses");
    TextView title = (TextView) findViewById(R.id.tv_select_enrollment);
    title.setText("Select your courses");
    adapter.setInstituteStudent(false);
    adapter.setCourses(courses);
}

private void renderInstitute(Institute institute) {
    getSupportActionBar().setTitle("Select Batches");
    TextView title = (TextView) findViewById(R.id.tv_select_enrollment);
    title.setText("Select your batches at " + institute.getName());
    adapter.setInstituteStudent(true);
    adapter.setBatches(institute.getBatches());

    // Save institute name
    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
    editor.putString("institute_name", institute.getName());
    editor.commit();
}

public void onButtonClick(View view) {
    findViewById(R.id.pb_loading).setVisibility(View.VISIBLE);
    findViewById(R.id.tv_error).setVisibility(View.GONE);
    findViewById(R.id.ll_content_area).setVisibility(View.GONE);
    if(adapter.isInstituteStudent()) {
        ArrayList<Batch> batches = adapter.getBatches();

        ArrayList<Integer> batchIds = new ArrayList<>();
        for(Batch batch: batches) {
            if(batch.isPreselect())
                batchIds.add(batch.getId());
        }
        GeneralApiService.addBatches(this, batchIds);
    } else {
        ArrayList<CourseSlug> courses = adapter.getCourses();
        ArrayList<String> courseSlgus = new ArrayList<>();
        for(CourseSlug course: courses) {
            if(course.isPreselect())
                courseSlgus.add(course.getSlug());
        }
        GeneralApiService.addCourses(this, courseSlgus);
    }
    new SavePlatformNamesTask().execute();
}
公共类SelectPlatform扩展了BaseActivity{
平台适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u select\u platform);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
初始化视图(
findViewById(R.id.ll_内容区),
findViewById(R.id.tv_错误),
findViewById(R.id.pb\U加载)
);
RecyclerView RecyclerView=(RecyclerView)findViewById(R.id.rv_平台);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
适配器=新平台适配器();
recyclerView.setAdapter(适配器);
私人课程(ArrayList课程){
getSupportActionBar().setTitle(“选择课程”);
TextView title=(TextView)findViewById(R.id.tv\u select\u注册);
title.setText(“选择您的课程”);
adapter.setInstituteStudent(false);
设置课程(courses);
}
私人机构(研究所){
getSupportActionBar().setTitle(“选择批次”);
TextView title=(TextView)findViewById(R.id.tv\u select\u注册);
title.setText(“在“+institute.getName()”中选择批次);
adapter.setInstituteStudent(true);
adapter.setBatches(institute.getBatches());
//保存机构名称
SharedReferences.Editor=PreferenceManager.GetDefaultSharedReferences(this.edit();
putString(“institute_name”,institute.getName());
commit();
}
公用作废按钮单击(查看){
findViewById(R.id.pb_加载).setVisibility(View.VISIBLE);
findViewById(R.id.tv_错误)。设置可见性(View.GONE);
findviewbyd(R.id.ll\u content\u area).setVisibility(View.go);
if(适配器.isInstituteStudent()){
ArrayList batches=adapter.getBatches();
ArrayList BatchId=新的ArrayList();
用于(批次:批次){
if(batch.isPreselect())
add(batch.getId());
}
addBatchs(这是batchId);
}否则{
ArrayList courses=adapter.getCourses();
ArrayList Courselgus=新的ArrayList();
适用于(课程附加课程:课程){
if(course.isPreselect())
courseSlgus.add(course.getSlug());
}
GeneralApiService.addCourses(这是courseSlgus);
}
新建SavePlatformNamesTask().execute();
}

像这样在适配器类中发送按钮引用。
public void setbatch(数组列表批次,按钮按钮)并根据需要执行任何操作。

选择平台中设置一个字段变量,并将其设置为
0

private int checkedBoxs = 0;
还可以在
Sel中公开方法
public void updateButtonState(int num){
   checkedBoxs+=num;
   if(checkedBoxs > 0){
     //Activate the BTN
   }else{
     //Deactivate the BTN
   }
}
adapter = new PlatformAdapter(SelectPlatform.this);
private SelectPlatform mThis;
public PlatformAdapter(SelectPlatform activity) {
    courses = new ArrayList<>();
    batches = new ArrayList<>();
    mThis = activity;
}
selectEnrollment.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(selectEnrollment.isChecked()) {
                  mThis.updateState(1);
                } else {
                  mThis.updateState(-1);
                }
        });
private MyInterface mListener;

public MyAdpater(Context context, MyInterface listener) {
    this.mListener = listener;
}

selectEnrollment.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                // make a callback
                mListener.onCheckBoxClick(true);

                int index = getLayoutPosition();
                if(isInstituteStudent) {
                    Batch batch = batches.get(index);
                    batch.setPreselect(b);
                } else {
                    CourseSlug course = courses.get(index);
                    course.setPreselect(b);

                        }
                    }


        });

public interface MyInterface {
    void onCheckBoxClick(boolean isAnyChecked); 
}
    public class MyActivity extends Activity implements MyAdapter.MyInterface {
    // other stuff....


    // Pass listener instance when initializing adapter
    MyAdapter adapter = new MyAdapter(MyActivity.this, this);


    @Override
    public void onCheckBoxClick(boolean isAnyChecked) {
        if(isAnyChecked) {
            // Activate button      
        } else {
            // Deactivate button
        }
    }
}