Skip to content

CheckBox state maintained while scrolling items in RecyclerView.

Notifications You must be signed in to change notification settings

jay5727/RecyclerViewCheckBox5727

Repository files navigation

RecyclerViewCheckBox5727

CheckBox state maintained while scrolling items in RecyclerView.

This sample code contains 3 files which can be found here: app/src/main/java/com/example/jay/recyclerviewcheckbox5727/

  • StudentActivity
  • StudentAdapter
  • StudentModel

StudentActivity

Responsible for loading dummy data from Students.json present inside assets folder.
Source can be REST API also depending on requirement.

StudentAdapter

An adapter manages the data model and adapts it to the individual entries in the widget.

It extends the RecyclerView.Adapter class and is assigned to the recycler view via the RecyclerView.setAdapter method from Activity/Fragment. The input to the adapter of an recycler view can be any List where T can be any model class itself.

Based on this input the adapter must return the total number of items via its getItemCount() method.

The adapter prepares the layout of the items by inflating the correct layout for the individual data elements. This work is done in the onCreateViewHolder method.

It returns an object of type ViewHolder per visual entry in the recycler view.

This instance is used to access the views in the inflated layout.

The onCreateViewHolder method is only called then a new view must be created.

Once a data item becomes visible, the adapter assigns this data to the individual widgets. This work is done in the onBindViewHolder method.

StudentModel

Data Source for RecyclerView.
Use your model to hold the current state of the view. You could have a boolean field in your model for the Recycler View to hold the state of your current item.So in your Model class you can have

private boolean isSelected;
public boolean isSelected() {
    return isSelected;
}

public void setSelected(boolean selected) {
    isSelected = selected;
}	

About

CheckBox state maintained while scrolling items in RecyclerView.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages