错误的部分 TAG 值
wrong section TAG value
我尝试从第一部分删除一些行,从第二部分删除一些行,但是
sectionAdapter.notifyItemRemovedFromSection(TAG, key);
TAG 总是 return 第二部分列表字符串 TAG,但我在第一个列表中被选中行
这里是完整的class代码:
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) {
Iterator it = selectedCheckBox.entrySet().iterator();
if (!it.hasNext()) {
new makeDialog().makeDialog(mActivity, mActivity.getResources().getString(R.string.share_no_data));
} else {
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
int key = (Integer) pair.getKey();
if ((Boolean) pair.getValue()) {
list.remove(key);
// problem Here
sectionAdapter.notifyItemRemovedFromSection(TAG, key);
}
}
chooseAllradioButton.setSelected(false);
}
}
});
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.getPositionInSection(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
class HeaderViewHolder extends RecyclerView.ViewHolder {
private final TextView tvTitle;
HeaderViewHolder(View view) {
super(view);
tvTitle = (TextView) view.findViewById(R.id.DishName);
}
}
class ItemViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener {
private final View rootView;
private final TextView tvItem;
private final CheckBox IntegBOX;
ItemViewHolder(View view) {
super(view);
rootView = view;
tvItem = (TextView) view.findViewById(R.id.DishName);
IntegBOX = (CheckBox) view.findViewById(R.id.chooseDish);
IntegBOX.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// int position = getAdapterPosition();
// list.get(position).checked = isChecked;
}
}
}
private class Item {
String name;
boolean checked;
Item(String name) {
this.name = name;
checked = false;
}
}
添加部分代码:
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()));
}
}
sectionAdapter = new SectionedRecyclerViewAdapter();
ArrayList<Item> contacts = new ArrayList<>();
// ArrayList<String> dishNames = new ArrayList<>();
mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
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);
注意:我在操作栏中设置了删除按钮,而不是在每一行中
所以当代码运行时,代码无法检测到当前行部分标记
您正在存储此 selectedCheckBox
地图中所有部分的所有选定复选框,但是当点击 deleteAll
按钮时,您会从它所在的部分调用 notifyItemRemovedFromSection
。
您必须更改您的 selectedCheckBox
映射以存储该复选框属于哪个部分 'tag',然后当您调用 notifyItemRemovedFromSection
时,您可以传递正确的 'tag'.
我尝试从第一部分删除一些行,从第二部分删除一些行,但是
sectionAdapter.notifyItemRemovedFromSection(TAG, key);
TAG 总是 return 第二部分列表字符串 TAG,但我在第一个列表中被选中行
这里是完整的class代码:
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) {
Iterator it = selectedCheckBox.entrySet().iterator();
if (!it.hasNext()) {
new makeDialog().makeDialog(mActivity, mActivity.getResources().getString(R.string.share_no_data));
} else {
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
int key = (Integer) pair.getKey();
if ((Boolean) pair.getValue()) {
list.remove(key);
// problem Here
sectionAdapter.notifyItemRemovedFromSection(TAG, key);
}
}
chooseAllradioButton.setSelected(false);
}
}
});
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.getPositionInSection(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
class HeaderViewHolder extends RecyclerView.ViewHolder {
private final TextView tvTitle;
HeaderViewHolder(View view) {
super(view);
tvTitle = (TextView) view.findViewById(R.id.DishName);
}
}
class ItemViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener {
private final View rootView;
private final TextView tvItem;
private final CheckBox IntegBOX;
ItemViewHolder(View view) {
super(view);
rootView = view;
tvItem = (TextView) view.findViewById(R.id.DishName);
IntegBOX = (CheckBox) view.findViewById(R.id.chooseDish);
IntegBOX.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// int position = getAdapterPosition();
// list.get(position).checked = isChecked;
}
}
}
private class Item {
String name;
boolean checked;
Item(String name) {
this.name = name;
checked = false;
}
}
添加部分代码:
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()));
}
}
sectionAdapter = new SectionedRecyclerViewAdapter();
ArrayList<Item> contacts = new ArrayList<>();
// ArrayList<String> dishNames = new ArrayList<>();
mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
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);
注意:我在操作栏中设置了删除按钮,而不是在每一行中 所以当代码运行时,代码无法检测到当前行部分标记
您正在存储此 selectedCheckBox
地图中所有部分的所有选定复选框,但是当点击 deleteAll
按钮时,您会从它所在的部分调用 notifyItemRemovedFromSection
。
您必须更改您的 selectedCheckBox
映射以存储该复选框属于哪个部分 'tag',然后当您调用 notifyItemRemovedFromSection
时,您可以传递正确的 'tag'.