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

时间:2017-02-17 11:24:20

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

我的Recyclerview和按钮实现类在下面给出

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();
}

3 个答案:

答案 0 :(得分:0)

像这样在适配器类中发送Button引用。 public void setBatches(ArrayList批处理,按钮按钮)并根据需要执行任何操作。

答案 1 :(得分:0)

SelectPlatform中设置字段变量并将其设置为0

private int checkedBoxs = 0;

也会在您的SelectPlatform中创建一个公共方法:

public void updateButtonState(int num){
   checkedBoxs+=num;
   if(checkedBoxs > 0){
     //Activate the BTN
   }else{
     //Deactivate the BTN
   }
}

修改适配器的调用以包含SelectPlatform

中的引用
adapter = new PlatformAdapter(SelectPlatform.this);

也不要忘记修改适配器以接收该引用,如下所示:

a)向适配器添加一个字段变量:

private SelectPlatform mThis;

b)修改构造函数:

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

最后在您的适配器中添加/修改onCheckedChanged的{​​{1}}并致电checkbox,如下所示:

updateButtonState

注意:该方法不必是公共的,但它必须具有允许适配器访问它的访问修饰符。

答案 2 :(得分:0)

您可以在适配器内设置一个接口,然后在您的包含活动的按钮中实现该接口,然后单击复选框上的事件,在活动中发送回调并执行您想要的任何更改。

示例:

MyAdapter.java

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); 
}

MyActivity.java

    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
        }
    }
}