Created
December 22, 2016 11:34
-
-
Save manabreak/301a0c16feaabaee887f32a170b1ebb4 to your computer and use it in GitHub Desktop.
A simple example of using checkboxes in recyclerview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.test_activity); | |
RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler); | |
recycler.setLayoutManager(new LinearLayoutManager(this)); | |
TestAdapter adapter = new TestAdapter(); | |
adapter.setCallback(new TestAdapter.Callback() { | |
@Override | |
public void onCheckedChanged(String item, boolean isChecked) { | |
// Do what you want :) | |
} | |
}); | |
adapter.addItem("Foo"); | |
adapter.addItem("Bar"); | |
adapter.notifyDataSetChanged(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is your reyclerview adapter | |
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> { | |
// Just storing strings for example | |
private List<String> items = new ArrayList<>(); | |
// A callback that gets invoked when an item is checked (or unchecked) | |
private Callback callback; | |
// Call this to add strings to the adapter | |
public void addItem(String item) { | |
items.add(item); | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_with_checkbox, parent, false)); | |
} | |
@Override | |
public int getItemCount() { | |
return items.size(); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
holder.bind(items.get(position)); | |
} | |
// Sets the callback | |
public void setCallback(Callback callback) { | |
this.callback = callback; | |
} | |
// Callback interface, used to notify when an item's checked status changed | |
public interface Callback { | |
void onCheckedChanged(String item, boolean isChecked); | |
} | |
// Our view holder | |
public static class ViewHolder extends RecyclerView.ViewHolder { | |
private CheckBox checkBox; | |
public ViewHolder(View itemView) { | |
super(itemView); | |
// Find the checkbox view in the layout | |
checkBox = (CheckBox) itemView.findViewById(R.id.checkbox); | |
} | |
void bind(String s) { | |
// Set the text | |
checkBox.setText(s); | |
// Listen to changes (i.e. when the user checks or unchecks the box) | |
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
// Invoke the callback | |
if(callback != null) callback.onCheckedChanged(s, isChecked); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment