mardi 4 février 2020

The fragment "row click" is triggered BEFORE the fragment row checkbox click

I have a RecyclerView in which every row represents a product. The row has 3 states, disabled (grey background), enabled (white background), free (green background). The free product shows a checkbox to set the product added to the cart by default (a sort of a bundle)

See image of Row states here

.

THE DESIDERED LOGIC IS:

  1. swipe left/right to disable/enable the row. (OK, DONE)
  2. if enabled, then you can click on the row to toggle it's state between the default "non free (white row)" and "free (green row)" (OK, DONE)
  3. If the row is green, a checkbox is shown. If you flag the checkbox, the product not only is free, but it will also be included in the bundle by default.(KO, NOT DONE YET)
  4. If you click a green row, the row is toggled to white and the checkbox must go hidden and unchecked because you cannot set a product to "free by default" if the product is marked as noN free (which happens when you toggle the row to white) (OK, DONE)

THE PROBLEM IS:

When the row is in green free mode, the checkbox is shown correctly, but if you click the checkbox to set it checked, the "row click" is triggered BEFORE the checkbox click, so the code behind the checked checkbox is never executed (and the checkbox correctly disappears)


THE CODE IS:

The row click is handled by the Fragment

recyclerView.addOnItemTouchListener(new CustomProductComponentEditFragment.RecyclerTouchListener(context,
            recyclerView, new CustomProductComponentEditFragment.ClickListener() {
        @Override
        public void onClick(View view, final int position) {
            //Values are passing to activity & to fragment as well
            setBtnSaveRed();
            componentRowContainer = view;
            CheckBox checkBox = view.findViewById(R.id.checkBox);
            /*
             * ROW CLICK LISTENER
             *
             * */
            if(cAdapter != null){
                if (product.getComponents().contains(componentsDataSource.get(position))) {
                    ComponentObj updatedComponent = componentsDataSource.get(position);
                    ComponentObj updatedProductComponent = getComponent(updatedComponent);

                    if (checkBox.getVisibility() == View.VISIBLE) {
                        updatedProductComponent.setFree(false);
                        updatedProductComponent.setHasdefault(false);
                        //checkBox.setChecked(false);
                        view.setBackgroundColor(Color.WHITE);
                        checkBox.setVisibility(View.INVISIBLE);
                    } else {
                        updatedProductComponent.setFree(true);
                        view.setBackgroundColor(Color.parseColor(GlobalSettings.getInstance().getStringValue("backgroundGridSelectedRow")));
                        checkBox.setVisibility(View.VISIBLE);
                    }
                    updateCustomComponentsProduct(updatedProductComponent, position);
                }
            }
        }

The checkbox click is handled by the adapter

public void onBindViewHolder(CustomProductComponentsAdapter.CustomProductComponentsViewHolder holder, int position) {
    ComponentObj componentObj = componentObjs.get(position);
    this.vHolder = holder;
    holder.title.setText(commonFunctions.capitalize(commonFunctions.localeName(componentObj.getId()).toLowerCase()));
    String compName = commonFunctions.localeName(componentObj.getId());
    checkBox = holder.checkBox;
    checkBox.setChecked(false);
    checkBox.setVisibility(View.INVISIBLE);

    /*
    * CHECKBOX CHANGE LISTENER
    *
    * */
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(productComponents == null) {
                return;
            }

            ComponentObj updatedComponentObj = componentObjs.get(position);
            if(isChecked)
            {
                holder.containerCardView.setBackgroundColor(Color.parseColor(GlobalSettings.getInstance().getStringValue("backgroundGridSelectedRow")));
                updatedComponentObj.setHasdefault(true);
                productComponents.add(updatedComponentObj);
            }
            else
            {
                holder.containerCardView.setBackgroundColor(Color.parseColor(GlobalSettings.getInstance().getStringValue("colorBackgroundActionButtonDisabledLight")));
                updatedComponentObj.setHasdefault(false);
                productComponents.remove(updatedComponentObj);
            }
            onComponentCheckedListener.onCustomChecked(buttonView, productComponents, isChecked);
        }

    });

    if(productComponents!= null && productComponents.contains(componentObj)){
        ComponentObj productComponent = getComponent(componentObj);
        // no must be productComponent
        if(productComponent.getFree()) {
            checkBox.setVisibility(View.VISIBLE);
            if(componentObj.getHasdefault()) {
                checkBox.setChecked(true);
            }
            holder.containerCardView.setBackgroundColor(Color.parseColor(GlobalSettings.getInstance().getStringValue("backgroundGridSelectedRow")));
        } else {
            holder.containerCardView.setBackgroundColor(Color.WHITE);
        }
    } else {
        holder.containerCardView.setBackgroundColor(Color.parseColor(GlobalSettings.getInstance().getStringValue("colorBackgroundActionButtonDisabledLight")));
        checkBox.setVisibility(View.INVISIBLE);
    }


}

I do not know what to do! Please someone help




Aucun commentaire:

Enregistrer un commentaire