我在这里使用了分区RecycleView库:
https://github.com/luizgrp/SectionedRecyclerViewAdapter
但我尝试做的是从第1部分和第2部分中选择一些多重复选框并点击删除按钮,部分是Addapter.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);
}
}
答案 0 :(得分:0)
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
的参数是项目在RecyclerView的整个项目列表中的位置,如果他们有这些项目的页眉/页脚,则会考虑这些项目的位置。
因此,假设您的RecyclerView看起来像这样:
Pos | Item
-----------------------
0 | Section 1 Item 0
1 | Section 1 Item 1
2 | Section 2 Item 0
3 | Section 2 Item 1
你的地图上有所有部分的所有项目:
notifyItemRemoved
当地图中位置0的项目被移除(“第1部分第0项”)时,您正在调用notifyItemRemoved
作为位置0,这是RecyclerView中的“第1部分标题”。但是,“第1节标题”未从RecyclerView中删除,这就是notifyItemRangeRemovedFromSection
无效的原因。
我建议你看一下example 8中 <ion-item-group *ngFor="let data of contraCheque">
<ion-item-divider color="light">{{ data[0].competencia }}</ion-item-divider> <!-- imprime o dado correto -->
<ion-item (click)="consultarCheque(data)">{{ data }}</ion-item> <!-- imprime todos os objetos de cada array -->
<ion-item (click)="consultarCheque(data)">{{ data[0].tipo }}</ion-item> <!-- imprime apenas a primeira prop de cada array -->
</ion-item-group>
的用法,并尝试做类似的事情。