0

I am trying to do same design in here. I have nested Recyclerview in my structure. I have BasketParentItem class and it keeps child products list. As you can see in here when i click the checkbox list expanding itself. This happens when i call notifyDataChangeSetChildAndParent() in listener. Any idea how to solve it ?

Edit: I solved problem. I removed divider on parent adapter now it's working well here

holder.rv_child_Basket_Parent.addItemDecoration(new DividerItemDecoration(mActivity.getApplicationContext(), DividerItemDecoration.VERTICAL));
holder.cb_Basket_CardView_Child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                
                if(list.get(position).isChecked()){

                    holder.cb_Basket_CardView_Child.setChecked(false);
                    list.get(position).setChecked(false);

                }else{
                    holder.cb_Basket_CardView_Child.setChecked(true);
                    list.get(position).setChecked(true);
                }

                
                boolean allCheck = false;

                for (BasketParentItem b : Basket.basketList) {
                    ArrayList<MyProduct> tempList = b.getChildItemList();

                    if (tempList.contains(list.get(position)) ) {

                        for (MyProduct mP : tempList) {

                            if(mP.isChecked()){
                                allCheck = true;
                            }
                        }

                        if(!allCheck){
                            //set parent uncheck
                            b.setChecked(false);
                        }

                    }

                }

               notifyDataChangeSetChildAndParent();
                
            }
        }); 
@Getter
@Setter
public class BasketParentItem {
    ArrayList<MyProduct> childItemList = new ArrayList<>();

    boolean checked=true;

    public void addToList(MyProduct myProduct){
        childItemList.add(myProduct);
    }
    public boolean removeFromList(MyProduct myProduct){ return childItemList.remove(myProduct); }


} 
@Getter
@Setter
@NoArgsConstructor
public class MyProduct {

    @NonNull
    String id; 
    @NonNull
    String name;
    @NonNull
    String description; 
    @NonNull
    String imageURL; 
    @NonNull
    String type; 
    @NonNull
    ArrayList<String> seller; 

    int stock; 
    double price; 
    Boolean fastDelivery; 
    int amount; 
    String sellerName;
    double sellerRate;

    boolean checked=true;

    //Returns selled Id
    public String getSellerId() {
        return seller.get(0);
    }
}
public class Basket {
    
    public static ArrayList<BasketParentItem> basketList = new ArrayList<>();
    public static ArrayList<String> bestSellerStringList = new ArrayList<>();
    public static ArrayList<MyProduct> bestSeller = new ArrayList<>();
}
public class BasketFragment extends Fragment {

//...

public void basketNotifyDataSetChanged(){
      if(!Basket.basketList.isEmpty()){
          ll_BasketFragment.setVisibility(View.GONE);
          rv_parent_FragmentBasket.setVisibility(View.VISIBLE);
      }else{
          ll_BasketFragment.setVisibility(View.VISIBLE);
          rv_parent_FragmentBasket.setVisibility(View.GONE);
      }
      if(parentAdapter != null){
          parentAdapter.notifyDataSetChanged();
      }
  }

  public void bestSellerNotifyDataSetChanged(){
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }
    }

}
private void notifyDataChangeSetChildAndParent(){
       BasketFragment basketFragment = (BasketFragment) ((MainActivity) mActivity).findFragment(MainActivity.BASKET_FRAGMENT);
       basketFragment.basketNotifyDataSetChanged();
       basketFragment.bestSellerNotifyDataSetChanged();
    }

Parent Row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="8dp"
    android:layout_marginBottom="6dp"
    android:layout_marginTop="0dp"
    android:background="@drawable/custom_background_3"
    android:elevation="3dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/cb_Basket_CardView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <TextView
            android:id="@+id/tv_Basket_Parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Satıcı"
            android:textColor="@color/grey"
            android:textSize="12sp"
            android:textStyle="bold" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/green_ice"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingVertical="2dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/ic_package" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/free_shipping"
            android:textSize="12sp"
            android:textStyle="bold" />

    </LinearLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_child_Basket_Parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="10dp"/>


</LinearLayout>

Child Row

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginVertical="14dp">

    <CheckBox
        android:id="@+id/cb_Basket_CardView_Child"
        android:layout_width="14dp"
        android:layout_height="15dp"
        android:layout_marginRight="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/iv_Basket_CardView_Child"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.367" />

    <com.google.android.material.imageview.ShapeableImageView

        android:id="@+id/iv_Basket_CardView_Child"
        android:layout_width="70dp"
        android:layout_height="102dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/linearLayout"
        app:layout_constraintStart_toEndOf="@+id/cb_Basket_CardView_Child"
        app:layout_constraintTop_toTopOf="parent"
        app:shapeAppearanceOverlay="@style/roundedImageViewRounded"
        app:srcCompat="@drawable/ic_home" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/iv_Basket_CardView_Child"
        app:layout_constraintTop_toTopOf="@+id/iv_Basket_CardView_Child"
        app:layout_constraintVertical_bias="0.0">

        <TextView
            android:id="@+id/tv_productName_Basket_CardView_Child"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="18dp"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="TextView"
            android:textSize="12sp"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/iv_deleteProduct_Basket_CardView_Child"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_marginLeft="15dp"
            app:srcCompat="@drawable/ic_trash" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:background="@drawable/custom_background_7"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/iv_Basket_CardView_Child"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="1.0">

        <ImageView
            android:id="@+id/iv_Decrease_CardView_Child"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_margin="2dp"
            android:padding="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_decrease_disabled" />

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

            <TextView
                android:id="@+id/tv_ProductAmount_Basket_CardView_Child"
                android:layout_width="28dp"
                android:layout_height="28dp"
                android:layout_marginVertical="2dp"
                android:background="@drawable/custom_background_8"
                android:gravity="center"
                android:maxLength="4"
                android:text="1"
                android:textColor="@color/pumpkin"
                android:textStyle="bold"
                app:layout_constraintTop_toTopOf="parent" />

            <ProgressBar
                android:id="@+id/pb_CardView_Child"
                android:layout_width="28dp"
                android:layout_height="28dp"
                android:layout_gravity="center"
                android:indeterminateTint="@color/pumpkin"
                android:visibility="invisible"/>

        </FrameLayout>


        <ImageView
            android:id="@+id/iv_Increase_CardView_Child"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_margin="2dp"
            android:padding="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_increase" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_FastDelivery_Basket_CardView_Child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:text="TextView"
        android:textSize="12sp"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/iv_Basket_CardView_Child"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/tv_Price_Basket_CardView_Child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TL"
        android:textColor="@color/pumpkin"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/linearLayout2"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="1.0" />


</androidx.constraintlayout.widget.ConstraintLayout>
Metehan
  • 21
  • 2
  • can you share the 'notifyDataChangeSetChildAndParent();' method – amm965 Jul 25 '22 at 09:07
  • And also please share the XML for the cells. – gioravered Jul 25 '22 at 09:09
  • I added both 'notifyDataChangeSetChildAndParent()' and XML for parent row and child row. 'basketFragment.bestSellerNotifyDataSetChanged()' in 'notifyDataChangeSetChildAndParent()' updates bestseller list which you can see at the bottom – Metehan Jul 25 '22 at 09:23
  • use this pro https://stackoverflow.com/questions/3669325/notifydatasetchanged-example – ahmed azam Jul 25 '22 at 09:55
  • I tried that but it didn't worked. I removed the divider in parent adapter and it worked. Thanks for helping me – Metehan Jul 25 '22 at 10:26

0 Answers0