无法处理分段的recyclerview中的删除多项选择 [英] can not handle delete multiple selection in sectioned recyclerview
问题描述
我在这里使用分段的RecycleView库:
I'm using sectioned RecycleView library from here :
https://github.com/luizgrp/SectionedRecyclerViewAdapter
但是我想做的是从第1部分和第2部分中选择多个复选框,然后点击remove按钮,sectionAdapter.notifyItemRemoved(key);.不做任何事情. 视图或行相同,则应对其进行更新并删除所选的行
but what I try to do is when select some multi check box from section 1 and section two and hit remove button , the sectionAdapter.notifyItemRemoved(key); dose not do anything . the view or row is the same , it should be updated and remove the selected row
这种情况下最好的实现方法是什么?
what is the best implementation for this case to use?
删除功能代码:
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);
}
}
推荐答案
notifyItemRemoved
的参数是该项目在RecyclerView整个项目列表中的位置,其中考虑了各节的页眉/页脚如果有的话.
The parameter for notifyItemRemoved
is the position of the item in the whole list of items of the RecyclerView, which takes into account the headers/footers of your sections if they have them.
因此,假设您的RecyclerView看起来像这样:
So let's say your RecyclerView looks like this:
Pos | Item
-----------------------
0 | Section 1 Header
1 | Section 1 Item 0
2 | Section 1 Item 1
3 | Section 2 Header
4 | Section 2 Item 0
5 | Section 2 Item 1
您的地图具有所有部分的所有项目:
And your Map has all the items of all the Sections:
Pos | Item
-----------------------
0 | Section 1 Item 0
1 | Section 1 Item 1
2 | Section 2 Item 0
3 | Section 2 Item 1
当地图中位置0的项被删除(第1部分0")时,您将调用位置0的notifyItemRemoved
,这是RecyclerView中的第1部分标题".但是没有从RecyclerView中删除"Section 1 Header",这就是notifyItemRemoved
无效的原因.
When the item from position 0 in your map gets removed ("Section 1 Item 0"), you are calling notifyItemRemoved
for position 0, which is the "Section 1 Header" in the RecyclerView. But "Section 1 Header" wasn't removed from the RecyclerView, that's why notifyItemRemoved
has no effect.
I would suggest you to have a look on how notifyItemRangeRemovedFromSection
is used in example 8 and try to do something similar.
这篇关于无法处理分段的recyclerview中的删除多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!