Vishal Kumar
Vishal Kumar

Reputation: 4627

How to populate a recyclerview on click of a button in a cardview

I am struck into a situation where I am finding no way to proceed. Actually I have a RecyclerView of cardViews and there is an adapter that populates the card with data. I have to show some restaurants in the card. On click of a button in the Cardview, I have to show another recyclerview that will be populated with cardViews containing the branches of that restaurant.

Here i have just one Model that can return both type of data. I am not getting whether I should use 1 Adapter with multiple viewHolders or 2 Adapters. My View is also a single layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/margin_medium">

    <android.support.v7.widget.CardView
        android:id="@+id/recipient_card"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:elevation="20dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/receipient_card_height"
                android:orientation="horizontal"
                android:id="@+id/linear_layout">


                <ImageView
                    android:id ="@+id/arrow_button"
                    android:layout_width="@dimen/size_info_icon"
                    android:layout_height="@dimen/size_info_icon"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    android:layout_gravity="center" />

                <com.inquirly.opinify.View.CustomTextView
                    android:id="@+id/business_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    style="@style/label_small"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    android:layout_gravity="center"
                    android:layout_weight="10"
                    android:text="JustBake"/>

                <ImageView
                    android:id="@+id/recipient_business_logo"
                    android:layout_marginLeft="@dimen/margin_small"
                    android:layout_gravity="center"
                    android:layout_width="@dimen/receipient_business_logo_size"
                    android:layout_height="@dimen/receipient_business_logo_size"/>


                <ImageView
                    android:id="@+id/expand_button"
                    android:layout_marginLeft="@dimen/margin_small"
                    android:layout_gravity="center"
                    android:padding="@dimen/margin_medium"
                    android:layout_width="@dimen/height_button"
                    android:layout_height="@dimen/height_button"
                    />

            </LinearLayout>

        </RelativeLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_recipient_branch_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:visible= "false"
        android:padding="@dimen/margin_medium"/>


</RelativeLayout>

I have made the inner recyclerView invisible in the beginning. When the user clicks on the arrow button in the main recycler's CardView button, the second recylerview should appear with the list of branches.

This is the screen shot of what i am trying to achieve. enter image description here

Upvotes: 2

Views: 1319

Answers (1)

Rakeeb Rajbhandari
Rakeeb Rajbhandari

Reputation: 5063

Why don't you do a implement a expandable RecyclerView instead. Check out this article. Also it's recommended that you check this question out.

Upvotes: 2

Related Questions