Jigar Adesara
Jigar Adesara

Reputation: 85

Add item in child recycleview from parent recycleview adapter android

I'm working on one project which requires to insert an item in child list from parent so,

I'm trying to add an item in child recycle view from parent recycle view adapter.

enter image description here

Is there any solution?

Thanks

Upvotes: 1

Views: 273

Answers (1)

Hossam Hassan
Hossam Hassan

Reputation: 791

You can do that by Creating 2 Adapters Main one will take the main array and the second Adapter will take the child array for example:

[
  {
    "id":1,
    "name":"name1",
    "items":
    [
      {"id":2,"name":"name2","valid":true},
      {"id":3,"name":"name3","valid":false},
      {"id":4,"name":"name4","valid":true}
    ]
  },
  {
    "id":5,
    "name":"name5",
    "items":
    [
      {"id":6,"name":"name6","valid":true},
      {"id":7,"name":"name7","valid":false}
    ]
  },
  {
    "id":8,
    "name":"name8",
    "items":
    [
      {"id":9,"name":"name9","valid":true},
      {"id":10,"name":"name10","valid":false},
      {"id":11,"name":"name11","valid":false},
      {"id":12,"name":"name12","valid":true}
    ]
  }
]

When you are setting the sub adapter you will say (items.get(postion).getItems) to be more clear check the code below.

in your main Adapter layout add another RecyclerView like this

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/main_category_layout"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="@color/white"
            android:visibility="visible">

            <ImageView
                android:id="@+id/category_pic"
                android:layout_width="37dp"
                android:layout_height="37dp"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginStart="20dp"
                android:adjustViewBounds="true"
                android:contentDescription="@string/nav_header_desc"
                android:scaleType="fitXY"
                android:src="@drawable/noimage"
                android:visibility="gone" />

            <TextView
                android:id="@+id/category_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="35dp"
                android:layout_toEndOf="@id/category_pic"
                android:gravity="start"
                android:singleLine="true"
                android:textColor="@color/black"
                android:textSize="13sp" />


            <ImageView
                android:id="@+id/category_arrow"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginEnd="10dp"
                android:background="@drawable/arrow_down"
                android:visibility="gone" />

        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height=".5dp"
            android:background="@color/white" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/secondRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>
</layout>

Now in your Main Adapter you need to initialise your second Adapter inside your onBindViewHolder

holder.binding.secondRecyclerView.setHasFixedSize(true);
                        holder.binding.secondRecyclerView.setLayoutManager(new LinearLayoutManager(context));
                        if (items.get(position).getChildren() != null) {
                            Sub_Adapter sub_adapter= new Sub_Adapter(items.get(position).getChildren(), context);
                            holder.binding.secondRecyclerView.setAdapter(sub_adapter);
                        }

And now all you have to do is to create the second Adapter (Sub_Adapter) and inflate different layout if you want or use the same one.

Upvotes: 1

Related Questions