title: Recyclerview inside Cardview
body: <p>i've been success create Recyclerview and Cardview.
  But now i need to create list data inside the Cardview, can i use Recyclerview inside Cardview ?</p>
  
  <p>Please help me, how to implement the new Adapter for Recyclerview that i have made below.</p>
  
  <p>This is my xml file for Cardview with Recyclerview inside it :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent" android:layout_height="match_parent"
      android:padding="3dp"&gt;
  
      &lt;android.support.v7.widget.CardView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/cv"&gt;
  
          &lt;RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="16dp"&gt;
  
              &lt;ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/person_photo"
                  android:layout_alignParentStart="true"
                  android:layout_alignParentTop="true"
                  android:layout_marginEnd="16dp"
                  android:contentDescription="@string/deskripsi"/&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/person_name"
                  android:layout_toEndOf="@+id/person_photo"
                  android:layout_alignParentTop="true"
                  android:textSize="30sp"/&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/person_age"
                  android:layout_toEndOf="@+id/person_photo"
                  android:layout_below="@+id/person_name"/&gt;
  
              &lt;android.support.v7.widget.RecyclerView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:id="@+id/rv"
                  android:layout_below="@+id/person_photo" /&gt;
  
          &lt;/RelativeLayout&gt; 
      &lt;/android.support.v7.widget.CardView&gt; 
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>and this is my adapter :</p>
  
  <pre><code>import java.util.List;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  import android.widget.ImageView;
  import android.support.v7.widget.CardView;
  import io.hidayat.iocardview.Person;
  
  public class RVAdapter extends RecyclerView.Adapter&lt;RVAdapter.PersonViewHolder&gt;{
  
      List&lt;Person&gt; persons;
  
      RVAdapter(List&lt;Person&gt; persons){
          this.persons = persons;
      }
  
      public static class PersonViewHolder extends RecyclerView.ViewHolder {      
          CardView cv;
          TextView personName;
          TextView personAge;
          ImageView personPhoto;
  
          PersonViewHolder(View itemView) {
              super(itemView);
              cv = (CardView)itemView.findViewById(R.id.cv);
              personName = (TextView)itemView.findViewById(R.id.person_name);
              personAge = (TextView)itemView.findViewById(R.id.person_age);
              personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
          }
      }
  
      @Override
      public int getItemCount() {
  
          return persons.size();
      }
  
      @Override
      public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
  
          personViewHolder.personName.setText(persons.get(i).name);
          personViewHolder.personAge.setText(persons.get(i).age);
          personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
      }
  
      @Override
      public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
  
          View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_list, viewGroup, false);
          PersonViewHolder pvh = new PersonViewHolder(v);
          return pvh;
      }
  
      @Override
      public void onAttachedToRecyclerView(RecyclerView recyclerView) {
  
          super.onAttachedToRecyclerView(recyclerView);
      }
  }
  </code></pre>
  
  <p>Then i create new Adapter for the Recyclerview inside cardview :</p>
  
  <pre><code>import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; {
  
      private ItemData[] itemsData;
  
      public MyAdapter(ItemData[] itemsData) {
  
          this.itemsData = itemsData;
      }
  
      // Create new views (invoked by the layout manager)
      @Override
      public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
  
          // create a new view
          View itemLayoutView = LayoutInflater.from(parent.getContext())
                                 .inflate(R.layout.item_view, parent, false);
  
          // create ViewHolder        
          ViewHolder viewHolder = new ViewHolder(itemLayoutView);
          return viewHolder;
      }
  
      // Replace the contents of a view (invoked by the layout manager)
      @Override
      public void onBindViewHolder(ViewHolder viewHolder, int position) {
  
          // - get data from your itemsData at this position
          // - replace the contents of the view with that itemsData         
          viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
          viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
      }
  
      // inner class to hold a reference to each item of RecyclerView 
      public static class ViewHolder extends RecyclerView.ViewHolder {
  
          public TextView txtViewTitle;
          public ImageView imgViewIcon;
  
          public ViewHolder(View itemLayoutView) {
  
              super(itemLayoutView);
              txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
              imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
          }
      }
  
  
      // Return the size of your itemsData (invoked by the layout manager)
      @Override
      public int getItemCount() {
  
          return itemsData.length;
      }
  }
  </code></pre>
  
  <p>this is the xml file for new Adapter :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="80dp"&gt;
  
       &lt;!-- icon --&gt;
       &lt;ImageView
           android:id="@+id/item_icon"
           android:layout_width="64dp"
           android:layout_height="64dp"
           android:layout_alignParentStart="true"
           android:layout_marginLeft="8dp"
           android:layout_marginRight="8dp"
           android:layout_marginTop="1dp"
           android:layout_marginBottom="1dp"
           android:contentDescription="@string/deskripsi"
           android:src="@drawable/ic_launcher"
       /&gt;
  
      &lt;!-- title --&gt;
      &lt;TextView
           android:id="@+id/item_title"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_toEndOf="@+id/item_icon"
           android:layout_alignBaseline="@+id/item_icon"
           android:textColor="@android:color/darker_gray"
            android:layout_marginLeft="8dp"
           android:layout_marginRight="8dp"
           android:layout_marginTop="8dp"
           android:textSize="22sp" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>main_activity.java :</p>
  
  <pre><code>package io.hidayat.iocardview;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.Menu;
  import android.view.MenuItem;
  
  import io.hidayat.iocardview.Person;
  
  public class MainActivity extends Activity {
  
      private List&lt;Person&gt; persons = initializeData();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_main);
          RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
          LinearLayoutManager llm = new LinearLayoutManager(this);
          rv.setLayoutManager(llm);
  
          RVAdapter adapter = new RVAdapter(persons);
          rv.setAdapter(adapter);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      private List&lt;Person&gt; initializeData(){
  
          persons = new ArrayList&lt;Person&gt;();
          persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ct_award));
          persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ct_belanja));
          persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ct_dipinjam));
          persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ct_award));
          persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ct_belanja));
          persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ct_dipinjam));
          persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ct_award));
          persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ct_belanja));
          persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ct_dipinjam));
          return persons;
      }
  }
  </code></pre>