I have a sectioned recycler view with 2 sections. They hold shopping list items in a recycler view, each item has a checkbox. I am using this library for sections: https://github.com/luizgrp/SectionedRecyclerViewAdapter
I am trying to set an onCheckedChangeListener for the checkboxes so if an item in section 1 becomes checked, it goes to section 2 and visa versa when checked->unchecked. I am starting with just trying to delete an item from section 1 when checked.
The ShoppingList class (Class A) creates the 2 instances of the HeaderRecyclerViewSection (Class B).
Shopping List class:
public class ShoppingList extends AppCompatActivity {
HeaderRecyclerViewSection firstSection = new HeaderRecyclerViewSection("Unchecked", getDataSource());
HeaderRecyclerViewSection secondSection = new HeaderRecyclerViewSection("Checked", getDataSource());
protected void onCreate(Bundle savedInstanceState) {
// shopping list with sections
sectionHeader = (RecyclerView) findViewById(R.id.shopping_listView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ShoppingList.this);
sectionHeader.setLayoutManager(linearLayoutManager);
sectionHeader.setHasFixedSize(true);
sectionAdapter = new SectionedRecyclerViewAdapter();
sectionAdapter.addSection(firstSection);
sectionAdapter.addSection(secondSection);
sectionHeader.setAdapter(sectionAdapter);
}
Section Class:
I need something in the onCheckedChangeListener to get the item that was clicked and remove it from the firstSection
, then notify the adapter in Shopping List.
public class HeaderRecyclerViewSection extends StatelessSection{
private static final String TAG = HeaderRecyclerViewSection.class.getSimpleName();
private String title;
public List<ShoppingItem> list;
public HeaderRecyclerViewSection(String title, List<ShoppingItem> list) {
super(SectionParameters.builder()
.itemResourceId(R.layout.shopping_item)
.headerResourceId(R.layout.section_header)
.build());
this.title = title;
this.list = list;
}
@Override
public int getContentItemsTotal() {
return list.size();
}
public boolean addItem(ShoppingItem shoppingItem) {
return list.add(shoppingItem);
}
public boolean removeItem(ShoppingItem shoppingItem) {
return list.remove(shoppingItem);
}
@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
return new ItemViewHolder(view);
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
final ItemViewHolder iHolder = (ItemViewHolder)holder;
final ShoppingItem currentItem = list.get(position);
iHolder.itemContent.setText(currentItem.getItemName());
iHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
removeItem(currentItem);
}
});
}
ItemViewHolder:
public class ItemViewHolder extends RecyclerView.ViewHolder{
public TextView itemContent;
public CheckBox checkBox;
public Button deleteButton;
public ItemViewHolder(View itemView) {
super(itemView);
itemContent = (TextView)itemView.findViewById(R.id.textView1);
checkBox = (CheckBox)itemView.findViewById(R.id.checkBox);
deleteButton = (Button)itemView.findViewById(R.id.deleteItem);
}
}