我有一个带有2个部分的分段回收视图。他们在回收者视图中保存购物清单项目,每个项目都有一个复选框。我正在使用这个库的部分: https://github.com/luizgrp/SectionedRecyclerViewAdapter
我正在尝试为复选框设置一个onCheckedChangeListener,所以如果第1部分中的某个项目被选中,它将进入第2部分,反之亦然,如果选中 - >未选中。我开始时只是在选中时尝试删除第1部分中的项目。
ShoppingList类(A类)创建HeaderRecyclerViewSection(B类)的2个实例。
购物清单类:
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);
}
章节类:
我需要onCheckedChangeListener中的某些内容来获取被点击的项目并将其从firstSection
中删除,然后通知购物列表中的适配器。
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);
}
}