notifydatasetchanged()如果被一行的复选框选中调用,则会导致循环

slwdgvem  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(166)

我有一个适配器,它绘制了一个产品列表和复选框,这些产品和复选框按名为firstrefallowgroup的产品属性分组。
通过选中其中一个复选框,具有相同firstrefallowgroup的行将保持启用状态,而其他行将被禁用,这意味着它们将以灰色文本重新绘制,并且复选框的可见性设置为gone。
要实现这一点,我使用checkbox.oncheckedchanged,它调用一个活动方法,该方法迭代所有产品,并根据firstrefallowgroup的值将名为tempdisabled的产品属性设置为true或false。然后我通知DataSetChanged(),不幸的是,它导致onbindviewholder->checkbox.setoncheckedchangelistener()再次运行,从而创建了一个循环。
长话短说:
适配器->onbindviewholder->绘制复选框->复选框.setoncheckedchangelistener()->activity.changeproductproperty->适配器.notifydatasetchanged()->适配器->绘制复选框。
扩展解释:
为了执行此操作,我添加了适配器的onbindviewholder()checkbox.setoncheckedchangelistener()方法。

// adapter.onBindViewHolder

// if product is tempDisabled, draw a disabled row

if (product != null && (product.isTempDisabled() || product.getEnabled() == null || product.getEnabled() == false ) ) {
        containerView.setAlpha((float) 0.5);
        productName.setPaintFlags(productName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        containerView.setBackgroundColor(Color.WHITE);
        checkBox.setVisibility(View.GONE);
    }

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            ConstraintLayout container = (ConstraintLayout) buttonView.getParent();

            if (product.getEnabled() == true) {
               if (isChecked) {
                 // customizeProductActivity is parent activity
                   customizeProductActivity.enableRefAllowGroupsProductsOnly(product.getFirstRefAllowGroup(), true);
                   } else {
                   customizeProductActivity.enableRefAllowGroupsProductsOnly(product.getFirstRefAllowGroup(), true);
                   }
             }
        }
    });

在父活动中,我添加了一个函数,根据单击的复选框的firstrefallowgroup属性将产品的属性“tempdisabled”设置为true或false。然后调用adapter.notifydatasetchanged()重新绘制行。如果产品的tempdisabled为true,则它将被绘制为disabled(灰色文本,无复选框),否则它将被绘制为enabled(已启用)。

//customizeProductActivity
 public void enableRefAllowGroupsProductsOnly(String refAllowGroup, Boolean checked){
        //productsAdapter
    Boolean doAction = true;
    Boolean unDoAction = false;
    if(checked == false){
        doAction = false;
        unDoAction = true;
    }
    if (this.entry.getProduct().products != null && this.entry.getProduct().products.size()>0) {

        HashMap<String, HashMap<String, SelectionGroup>> groupsMap = menuObj.getGroups();

        // products ordered by int id (asc)
        ArrayList<ProductObj> productProductList = productRepository.getProductsInteger(this.entry.getProduct().products);

        Iterator<ProductObj> it = productProductList.iterator();

        while (it.hasNext()) {

            ProductObj productObj =  (ProductObj) it.next();
            String allowedGroup = productObj.getFirstRefAllowGroup();
            if(!allowedGroup.equals(refAllowGroup)){
                productObj.setTempDisabled(doAction);
            } else {
                productObj.setTempDisabled(unDoAction);
            }
        }

        mRecyclerView.post(new Runnable()
                           {
                               @Override
                               public void run() {
                                   adapter.notifyDataSetChanged();
                               }
                           }
        );
    }
}

罪犯来了。notifydatasetchanged()将导致适配器重新绘制每个复选框,因此再次点击checkbox.setoncheckedchangelistener()方法,从而导致循环。
问题是:如何通过选中其中一个复选框来启用/禁用某些行,而不会导致循环?
请把我弄出去!:-)
谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题