-1

我在这里使用分段的 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);
        }
    }
4

1 回答 1

0

参数 fornotifyItemRemoved是项目在 RecyclerView 的整个项目列表中的位置,如果有节的页眉/页脚,则将其考虑在内。

因此,假设您的 RecyclerView 如下所示:

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

您的地图包含所有部分的所有项目:

Pos | Item
-----------------------
0   | Section 1 Item 0
1   | Section 1 Item 1
2   | Section 2 Item 0
3   | Section 2 Item 1

当地图中位置 0 的项目被移除(“Section 1 Item 0”)时,您正在调用notifyItemRemoved位置 0,即 RecyclerView 中的“Section 1 Header”。但是“Section 1 Header”没有从 RecyclerView 中删除,这就是notifyItemRemoved没有效果的原因。

我建议你看看如何notifyItemRangeRemovedFromSection示例 8中使用并尝试做类似的事情。

于 2017-04-19T17:54:51.663 回答