1

我正在使用 SectionedRecyclerViewAdapter 并传递给 myAdapter 两个部分

第一部分有 16 个元素,第二部分有 18 个元素

每个元素都有 CheckBox 项目,我正在尝试调用将所有项目设置为已选中的方法,但是当循环它并获取项目编号 17 时,它返回 null

但我的 mRecyclerView 实际上总共有 34 个项目

那么如何移动到其他部分并获取元素编号 17。

            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++) {
                contacts.add(new Item(DishesIngredientsShoppingList.get(d).getGroups()[i].getIngredients()[g].getName()));
            }
        }
        sectionAdapter.addSection(new ContactsSection(DishesIngredientsShoppingList.get(d).getName(),contacts));
   //     contacts.clear();
    }


  class ContactsSection extends StatelessSection {

    String title;
    List<Item> list;
    ItemViewHolder row;
    HeaderViewHolder MainRow;
    public ContactsSection(String title, ArrayList<Item> list) {
        super(R.layout.shopping_row_item_header, R.layout.shopping_row_item);

        this.title = title;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size(); // list here has 16 elements !! it should 34 elements 
    }

    @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);



        chooseAllradioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {




                for (int i = 0; i <sectionAdapter.getItemCount(); i++) {
                    final Item name = list.get(i);
                    name.checked = true;
                    sectionAdapter.notifyDataSetChanged();
                }



            }
        });

    @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);
    }
    }

    class HeaderViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvTitle;
        private final CheckBox DishBOX;
        public HeaderViewHolder(View view) {
            super(view);

            tvTitle = (TextView) view.findViewById(R.id.DishName);
            DishBOX = (CheckBox) view.findViewById(R.id.chooseDish);
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener{

        private final View rootView;
        private final TextView tvItem;
        private final CheckBox IntegBOX;
        public 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;
        }
    }


    }
class Item {
    String name;
    boolean checked;

    public Item(String name) {
        this.name = name;
        checked = false;
    }
}
4

1 回答 1

0

在 ContactsSection 类中添加一个方法来检查所有项目:

public void checkAllItems() {
    for (Item item : list) {
        item.checked = true;
    }
}

然后将您的 onClick 代码更改为:

chooseAllradioButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        for(Section section : sectionAdapter.getSectionsMap().values()) {
            section.checkAllItems();    
        }
        sectionAdapter.notifyDataSetChanged();

    }
});
于 2016-07-21T20:51:14.513 回答