我在这里使用分段的 RecycleView 库:
https://github.com/luizgrp/SectionedRecyclerViewAdapter
但我尝试做的是从第 1 节和第 2 节中选择一些多复选框并点击删除按钮,sectionAdapter.notifyItemRemoved(key); 不要做任何事情。视图或行相同,应更新并删除所选行
这种情况下使用的最佳实现是什么?
删除功能代码:
private void getDishesIngredientsShoppingList() {
appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(mActivity);
Gson gson = new Gson();
String json2 = appSharedPrefs.getString("DishesIngredientsShoppingList", "");
Type type2 = new TypeToken<ArrayList<Dishes>>() {
}.getType();
DishesIngredientsShoppingList = gson.fromJson(json2, type2);
// Check if ArrayList null
if (DishesIngredientsShoppingList == null) {
DishesIngredientsShoppingList = new ArrayList<>();
}
sectionAdapter = new SectionedRecyclerViewAdapter();
ArrayList<Item> contacts = new ArrayList<>();
// ArrayList<String> dishNames = new ArrayList<>();
mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
for (int d = 0; d < DishesIngredientsShoppingList.size(); d++) {
contacts = new ArrayList<>();
for (int i = 0; i < DishesIngredientsShoppingList.get(d).getGroups().length; i++) {
for (int g = 0; g < DishesIngredientsShoppingList.get(d).getGroups()[i].getIngredients().length; g++) {
if (Language)
contacts.add(new Item(DishesIngredientsShoppingList.get(d).getGroups()[i].getIngredients()[g].getName()));
else
contacts.add(new Item(DishesIngredientsShoppingList.get(d).getGroups()[i].getIngredients()[g].getNameEnglish()));
}
}
if (Language)
sectionAdapter.addSection(DishesIngredientsShoppingList.get(d).getName(), new ContactsSection(DishesIngredientsShoppingList.get(d).getName(),contacts));
else
sectionAdapter.addSection(DishesIngredientsShoppingList.get(d).getEnglishName(), new ContactsSection(DishesIngredientsShoppingList.get(d).getEnglishName(),contacts));
}
mRecyclerView.setAdapter(sectionAdapter);
}
class ContactsSection extends StatelessSection {
final String TAG;
String title;
List<Item> list;
ItemViewHolder row;
HeaderViewHolder MainRow;
ContactsSection(String title, ArrayList<Item> list) {
super(R.layout.shopping_row_item_header, R.layout.shopping_row_item);
this.title = title;
this.list = list;
this.TAG = title;
}
@Override
public int getContentItemsTotal() {
return list.size();
}
@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
return new ItemViewHolder(view);
}
@Override
public void onBindItemViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final ItemViewHolder itemHolder = (ItemViewHolder) holder;
final Item name = list.get(position);
itemHolder.tvItem.setText(name.name);
((ItemViewHolder) holder).IntegBOX.setChecked(name.checked);
((ItemViewHolder) holder).IntegBOX.setTag(position);
deleteAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
removeAtAll();
}
});
itemHolder.IntegBOX.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Set the button's appearance
((CheckBox) v).setChecked(!((CheckBox) v).isChecked());
// if button Already in checked statue
if (((CheckBox) v).isChecked()) {
Iterator it = selectedCheckBox.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
int key = (Integer) pair.getKey();
if (key == (Integer) v.getTag()) {
it.remove();
}
// set it as false
((CheckBox) v).setChecked(false);
}
} else {
// Otherwise set it to true and add it to hashMap
((CheckBox) v).setChecked(true);
selectedCheckBox.put((Integer) v.getTag(), true);
}
Toast.makeText(mActivity, String.format("Clicked on position #%s of Section %s", sectionAdapter.getSectionPosition(itemHolder.getAdapterPosition()), title), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new HeaderViewHolder(view);
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.tvTitle.setText(title);
}
// Remove All Btn Fuc
void removeAtAll() {
Iterator it = selectedCheckBox.entrySet().iterator();
if (!it.hasNext()) {
new makeDialog().makeDialog(mActivity, mActivity.getResources().getString(R.string.share_no_data));
} else {
// int hesham = sectionAdapter.getSectionForPosition(0).getContentItemsTotal();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
int key = (Integer) pair.getKey();
if ((Boolean) pair.getValue()) {
int contentItemsTotal = getContentItemsTotal();
sectionAdapter.notifyItemRangeRemovedFromSection(TAG, 0, contentItemsTotal);
// int positionInSection = sectionAdapter.getPositionInSection(key);
// list.remove(positionInSection);
// sectionAdapter.notifyItemRemovedFromSection(TAG, 2);
// DishToDel.add(dish.get(key).getDishID());
// sectionAdapter.getSectionForPosition(0).getContentItemsTotal();
// list.remove(key);
// it.remove();
/* hesham--;*/
// sectionAdapter.notifyItemRemoved(key);
}
// sectionAdapter.notifyItemRangeChanged(key, hesham);
}
chooseAllradioButton.setSelected(false);
}
}