我正在使用SectionedRecyclerViewAdapter,并且将两部分传递给myAdapter

第一部分包含16个元素,第二部分包含18个元素

每个元素都有CheckBox Item,我尝试调用将所有项目设置为选中的方法,但是在其上循环并获取项目编号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;
    }
}

最佳答案

在您的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();

    }
});

关于java - findViewHolderForAdapterPosition返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37220958/

10-12 05:52