I am having one recycler view with an adapter class. In the single item view there is a checkbox also. In my layout view it contains one button and recycler view mentioned above. When i checks on any check box in the view the button should get activated (Changing the color to active). When nothing is selected on the view button should become in the form of deactivated. How can i possible? Any example? Code is below
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);
}
}
My Recyclerview and button implemented class is given below
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();
}
Aucun commentaire:
Enregistrer un commentaire