【问题标题】:Select only one checkbox in section recylerview?在部分recyclerview中只选择一个复选框?
【发布时间】:2018-01-17 04:06:23
【问题描述】:

我正在使用分段回收视图来添加到购物车屏幕。并且想从类别的各种项目中一次只选择一个复选框。下面是代码和屏幕图像。请给我解决方案,从产品类别中一次只选择一个复选框。

下面是json响应,

{
 "Status":"Success",
 "StatusCode":"200",
 "Message":"data fetch successfully.",
 "Data":{
  "1":{
     "OptionGroupName":"Base",
     "OptionGroupId":"1",
     "OptionCount":2,
     "Options":[
        {
           "OptionGroupId":"1",
           "OptionGroupName":"Base",
           "ProductId":"54",
           "OptionId":"1",
           "OptionName":"Soft",
           "OptionPrice":"25",
           "IsActive":"1"
        },
        {
           "OptionGroupId":"1",
           "OptionGroupName":"Base",
           "ProductId":"54",
           "OptionId":"2",
           "OptionName":"Hard",
           "OptionPrice":"15",
           "IsActive":"1"
        }
     ]
  },
  "2":{
     "OptionGroupName":"Sauce",
     "OptionGroupId":"2",
     "OptionCount":3,
     "Options":[
        {
           "OptionGroupId":"2",
           "OptionGroupName":"Sauce",
           "ProductId":"54",
           "OptionId":"3",
           "OptionName":"Shezwan",
           "OptionPrice":"10",
           "IsActive":"1"
        },
        {
           "OptionGroupId":"2",
           "OptionGroupName":"Sauce",
           "ProductId":"54",
           "OptionId":"4",
           "OptionName":"Chilly",
           "OptionPrice":"20",
           "IsActive":"1"
        },
        {
           "OptionGroupId":"2",
           "OptionGroupName":"Sauce",
           "ProductId":"54",
           "OptionId":"5",
           "OptionName":"Soya",
           "OptionPrice":"29",
           "IsActive":"1"
        }
     ]
  }
  }
  }

这是java代码;

  public class ProductCartAdapter extends SectionedRecyclerViewAdapter<RecyclerView.ViewHolder> {
    private List<ProductCart> allData;

    public ProductCartAdapter(List<ProductCart> data) {
        this.allData = data;
    }

    @Override
    public int getSectionCount() {
        return allData.size();
    }

    @Override
    public int getItemCount(int section) {
        return allData.get(section).getItemList().size();
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int section) {
        String sectionName = allData.get(section).getName();
        SectionViewHolder sectionViewHolder = (SectionViewHolder) holder;
        sectionViewHolder.tvProductName.setText(sectionName);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int section, final int relativePosition, final int absolutePosition) {
        final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.tvProductName.setText(allData.get(section).getItemList().get(relativePosition).getOptionName());
        itemViewHolder.tvProductPrice.setText("+ $" + allData.get(section).getItemList().get(relativePosition).getOptionPrice());

        **// what I have to code here for selecting only one checkbox (not from addons only from base and sauce)**

        itemViewHolder.cbProduct.setTag(section);
        itemViewHolder.cbProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Integer pos = (Integer) itemViewHolder.cbProduct.getTag();

                if (allData.get(section).getItemList().get(relativePosition).isSelected()) {
                    allData.get(pos).getItemList().get(relativePosition).setSelected(false);
                } else {
                    allData.get(pos).getItemList().get(relativePosition).setSelected(true);
                }
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, boolean header) {
        View v = null;
        if (header) {
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_section_cart, parent, false);
            return new SectionViewHolder(v);
        } else {
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_cart_item, parent, false);
            return new ItemViewHolder(v);
        }

    }

    // SectionViewHolder Class for Sections
    public class SectionViewHolder extends RecyclerView.ViewHolder {
        final TextView tvProductName;

        public SectionViewHolder(View itemView) {
            super(itemView);
            tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
        }
    }

    // ItemViewHolder Class for Items in each Section
    public class ItemViewHolder extends RecyclerView.ViewHolder {

        final TextView tvProductName, tvProductPrice;
        CheckBox cbProduct;

        public ItemViewHolder(final View itemView) {
            super(itemView);
            tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
            tvProductPrice = (TextView) itemView.findViewById(R.id.tvProductPrice);
            cbProduct = (CheckBox) itemView.findViewById(R.id.cbProduct);
        }
    }

}

【问题讨论】:

  • stackoverflow.com/a/4844207/6701660 试试这个方法是正确的
  • 您可以跟踪选中 CheckBox 的当前位置,并在选中任何其他 CheckBox 时更新以前的值并刷新适配器。
  • 所以你想从类别或所有列表中一次检查单个复选框?

标签: android listview android-recyclerview recyclerview-layout


【解决方案1】:
      public class ProductCartAdapter extends SectionedRecyclerViewAdapter<RecyclerView.ViewHolder> {
        private List<ProductCart> allData;

        public ProductCartAdapter(List<ProductCart> data) {
            this.allData = data;
        }

        @Override
        public int getSectionCount() {
            return allData.size();
        }

        @Override
        public int getItemCount(int section) {
            return allData.get(section).getItemList().size();
        }

        @Override
        public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int section) {
            String sectionName = allData.get(section).getName();
            SectionViewHolder sectionViewHolder = (SectionViewHolder) holder;
            sectionViewHolder.tvProductName.setText(sectionName);
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int section, final int relativePosition, final int absolutePosition) {
            final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            itemViewHolder.tvProductName.setText(allData.get(section).getItemList().get(relativePosition).getOptionName());
            itemViewHolder.tvProductPrice.setText("+ $" + allData.get(section).getItemList().get(relativePosition).getOptionPrice());

            **// what I have to code here for selecting only one checkbox (not from addons only from base and sauce)**

            itemViewHolder.cbProduct.setTag(section);
            itemViewHolder.cbProduct.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    resetCategoryWiseData(section);
                    Integer pos = (Integer) itemViewHolder.cbProduct.getTag();

                    if (allData.get(section).getItemList().get(relativePosition).isSelected()) {
                        allData.get(pos).getItemList().get(relativePosition).setSelected(false);
                    } else {
                        allData.get(pos).getItemList().get(relativePosition).setSelected(true);
                    }
    notifyItemChanged(pos);
                }
            });

    *********************************************************

        //code for checked/unchecked
        if(allData.get(section).getItemList().get(relativePosition).getSelected()){
        itemViewHolder.cbProduct.setChecked(true);
        }else{
        itemViewHolder.cbProduct.setChecked(false);
        }

    **************************************************************
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, boolean header) {
            View v = null;
            if (header) {
                v = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_section_cart, parent, false);
                return new SectionViewHolder(v);
            } else {
                v = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_cart_item, parent, false);
                return new ItemViewHolder(v);
            }

        }

        // SectionViewHolder Class for Sections
        public class SectionViewHolder extends RecyclerView.ViewHolder {
            final TextView tvProductName;

            public SectionViewHolder(View itemView) {
                super(itemView);
                tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
            }
        }

        // ItemViewHolder Class for Items in each Section
        public class ItemViewHolder extends RecyclerView.ViewHolder {

            final TextView tvProductName, tvProductPrice;
            CheckBox cbProduct;

            public ItemViewHolder(final View itemView) {
                super(itemView);
                tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
                tvProductPrice = (TextView) itemView.findViewById(R.id.tvProductPrice);
                cbProduct = (CheckBox) itemView.findViewById(R.id.cbProduct);
            }
        }

private void  resetCategoryWiseData(int section){
    for(int i=0;i<allData.get(section).getItemList().size();i++){
         allData.get(section).getItemList().get(i).setSelected(false);
     }
  }

add this line  **notifyItemChanged(pos);** in click listener of checkox.

and add some code that i have already written in above for ( checkbox is checked or not )


if any issue then tell me.Happy Coding.:)

【讨论】:

  • 通过使用此代码,我可以选择任何复选框。我只想从类别基础和酱汁中一次只选择一个复选框
  • 您有 2 种方法可以做到这一点。1) 通过公共变量在 adpter 中存储先前选择的位置并取消选中该位置。2) 使用该类别的 for 循环未选中先前位置。如果您不明白然后告诉我,我给你一个代码。
  • 请您告诉我如何在选择另一个位置后取消选中特定部分中的先前位置?
  • 我添加了 1 个方法..resetCategoryWiseData().请检查我编辑的答案
  • 我仍然可以选择多个值
【解决方案2】:

如果您只想检查一项,请改用 RadioGroup 或 recyclerview(在其中一项)。

这样用户就会知道他/她只能从列表中选择一项。这将提高用户的理解,也将减少再次检查任何项目是否已检查的循环。

【讨论】:

    猜你喜欢
    • 2017-08-10
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多