title: Dynamic Different Images Horizontal RecyclerView in Vertical RecyclerView like Google Play Store
body: <p>I have been following this tutorial: </p>
  
  <p><a href="http://android-pratap.blogspot.co.za/2015/12/horizontal-recyclerview-in-vertical.html" rel="nofollow">http://android-pratap.blogspot.co.za/2015/12/horizontal-recyclerview-in-vertical.html</a></p>
  
  <p>All the code is on the blog. What I am trying to implement differently is, instead of loading the same image over and over again, I want to pull 3 sets of images from 3 different <code>ArrayLists</code>. So far, I was using 1 <code>ArrayList</code> and instead of loading 18 images, the same 6 images are being loaded 3 times, so it's 3 duplicate sets of images, instead of unique images in all 3 rows.</p>
  
  <p><strong>This is what I would like</strong> (if possible from 3 arraylists):</p>
  
  <blockquote>
    <p>img0   img1   img2   img3   img4   img5</p>
    
    <p>img6   img7   img8   img9   img10  img11</p>
    
    <p>img12  img13  img14  img15  img16  img17</p>
  </blockquote>
  
  <p><strong>This is what I am getting:</strong></p>
  
  <blockquote>
    <p>img0   img1   img2   img3   img4   img5</p>
    
    <p>img0   img1   img2   img3   img4   img5</p>
    
    <p>img0   img1   img2   img3   img4   img5</p>
  </blockquote>
  
  <p>Here is the code I edited:</p>
  
  <p><strong>DownloadImageTask.java</strong></p>
  
  <pre><code>import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.os.AsyncTask;
  import android.widget.ImageView;
  
  import java.io.InputStream;
  
  public class DownloadImageTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
      ImageView bmImage;
      public DownloadImageTask(ImageView bmImage) {
          this.bmImage = bmImage;
      }
      protected Bitmap doInBackground(String... urls) {
          String urlDisplay = urls[0];
          Bitmap mIcon11 = null;
          try {
              InputStream in = new java.net.URL(urlDisplay).openStream();
              mIcon11 = BitmapFactory.decodeStream(in);
          } catch (Exception e) {
              e.printStackTrace();
          }
          return mIcon11;
      }
      protected void onPostExecute(Bitmap result) {
          bmImage.setImageBitmap(result);
      }
  }
  </code></pre>
  
  <p><strong>SectionListDataAdapter.java</strong></p>
  
  <pre><code>import android.content.Context;
  import android.support.v7.widget.RecyclerView;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  
  import com.mypackage.appname.R;
  import com.mypackage.appname.common.asyncTasks.DownloadImageTask;
  import import com.mypackage.appname.common.models.SingleItemModel;
  
  public class SectionListDataAdapter extends RecyclerView.Adapter&lt;SectionListDataAdapter.SingleItemRowHolder&gt; {
  
      private ArrayList&lt;SingleItemModel&gt; itemsList;
      private Context mContext;
      //Images Start
      ArrayList&lt;String&gt; urls = new ArrayList&lt;&gt;();
      //Download Images
      DownloadImageTask dit;
      //Images End
  
      public SectionListDataAdapter(Context context, ArrayList&lt;SingleItemModel&gt; itemsList) {
          this.itemsList = itemsList;
          this.mContext = context;
      }
  
      @Override
      public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
          View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, null);
          SingleItemRowHolder mh = new SingleItemRowHolder(v);
          return mh;
      }
  
      @Override
      public void onBindViewHolder(SingleItemRowHolder holder, int i) {
          SingleItemModel singleItem = itemsList.get(i);
          holder.tvTitle.setText(singleItem.getName());
          urls.add("http:mywebsite.com/images/d"+i+".jpg");
          //Set Splash Images START
          if(holder.itemImage!=null){
              dit = new DownloadImageTask(holder.itemImage);
              Log.e("itemImage", String.valueOf(holder.itemImage));
              try{
                  Log.e("imageUrls", urls.get(i));
                  dit.execute(urls.get(i));
              }
              catch (NullPointerException e){
                  e.printStackTrace();
              }
          }
      }
  
      @Override
      public int getItemCount() {
          return (null != itemsList ? itemsList.size() : 0);
      }
      public class SingleItemRowHolder extends RecyclerView.ViewHolder {
          protected TextView tvTitle;
          protected ImageView itemImage;
          public SingleItemRowHolder(View view) {
              super(view);
              this.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
              this.itemImage = (ImageView) view.findViewById(R.id.itemImage);
              view.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Toast.makeText(v.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show();
                  }
              });
          }
      }
  
  }
  </code></pre>
  
  <p>RecyclerViewDataAdapter.java</p>
  
  <pre><code>import android.content.Context;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.pratap.gplaystore.R;
  import com.pratap.gplaystore.models.SectionDataModel;
  
  import java.util.ArrayList;
  
  public class RecyclerViewDataAdapter extends RecyclerView.Adapter&lt;RecyclerViewDataAdapter.ItemRowHolder&gt; {
  
      private ArrayList&lt;SectionDataModel&gt; dataList;
      private Context mContext;
  
      public RecyclerViewDataAdapter(Context context, ArrayList&lt;SectionDataModel&gt; dataList) {
          this.dataList = dataList;
          this.mContext = context;
      }
  
      @Override
      public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
          View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
          ItemRowHolder mh = new ItemRowHolder(v);
          return mh;
      }
  
      @Override
      public void onBindViewHolder(ItemRowHolder itemRowHolder, int i) {
  
          final String sectionName = dataList.get(i).getHeaderTitle();
  
          ArrayList singleSectionItems = dataList.get(i).getAllItemsInSection();
  
          itemRowHolder.itemTitle.setText(sectionName);
  
          SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, singleSectionItems);
  
          itemRowHolder.recycler_view_list.setHasFixedSize(true);
          itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
          itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);
  
  
          itemRowHolder.btnMore.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
  
                  Toast.makeText(v.getContext(), "click event on more, "+sectionName , Toast.LENGTH_SHORT).show();
  
  
  
              }
          });
  
  
         /* Glide.with(mContext)
                  .load(feedItem.getImageURL())
                  .diskCacheStrategy(DiskCacheStrategy.ALL)
                  .centerCrop()
                  .error(R.drawable.bg)
                  .into(feedListRowHolder.thumbView);*/
      }
  
      @Override
      public int getItemCount() {
          return (null != dataList ? dataList.size() : 0);
      }
  
      public class ItemRowHolder extends RecyclerView.ViewHolder {
  
          protected TextView itemTitle;
  
          protected RecyclerView recycler_view_list;
  
          protected Button btnMore;
  
  
  
          public ItemRowHolder(View view) {
              super(view);
  
              this.itemTitle = (TextView) view.findViewById(R.id.itemTitle);
              this.recycler_view_list = (RecyclerView) view.findViewById(R.id.recycler_view_list);
              this.btnMore= (Button) view.findViewById(R.id.btnMore);
  
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>SingleItemModel.java</p>
  
  <pre><code>public class SingleItemModel {
  
  
      private String name;
      private String url;
      private String description;
  
  
      public SingleItemModel() {
      }
  
      public SingleItemModel(String name, String url) {
          this.name = name;
          this.url = url;
      }
  
  
      public String getUrl() {
          return url;
      }
  
      public void setUrl(String url) {
          this.url = url;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
  
  }
  </code></pre>
  
  <p>MainActivity.java</p>
  
  
  
  <pre class="lang-js prettyprint-override"><code>import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.support.v7.widget.Toolbar;
  
  import com.pratap.gplaystore.adapters.RecyclerViewDataAdapter;
  import com.pratap.gplaystore.models.SectionDataModel;
  import com.pratap.gplaystore.models.SingleItemModel;
  
  import java.util.ArrayList;
  
  public class MainActivity extends AppCompatActivity {
  
      private Toolbar toolbar;
  
  
      ArrayList&lt;SectionDataModel&gt; allSampleData;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          toolbar = (Toolbar) findViewById(R.id.toolbar);
  
          allSampleData = new ArrayList&lt;SectionDataModel&gt;();
  
          if (toolbar != null) {
              setSupportActionBar(toolbar);
              toolbar.setTitle("G PlayStore");
  
          }
  
  
          createDummyData();
  
  
          RecyclerView my_recycler_view = (RecyclerView) findViewById(R.id.my_recycler_view);
  
          my_recycler_view.setHasFixedSize(true);
  
          RecyclerViewDataAdapter adapter = new RecyclerViewDataAdapter(this, allSampleData);
  
          my_recycler_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
  
          my_recycler_view.setAdapter(adapter);
  
  
      }
  
      public void createDummyData() {
          for (int i = 1; i &lt;= 5; i++) {
  
              SectionDataModel dm = new SectionDataModel();
  
              dm.setHeaderTitle("Section " + i);
  
              ArrayList&lt;SingleItemModel&gt; singleItem = new ArrayList&lt;SingleItemModel&gt;();
              for (int j = 0; j &lt;= 5; j++) {
                  singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
              }
  
              dm.setAllItemsInSection(singleItem);
  
              allSampleData.add(dm);
  
          }
      }
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary"
          android:elevation="8dp"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /&gt;
  
  
      &lt;android.support.v7.widget.RecyclerView
          android:id="@+id/my_recycler_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:scrollbars="none" /&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>list_item.xml</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="wrap_content"
  
      android:background="?android:selectableItemBackground"
      android:orientation="vertical"
      android:padding="5dp"&gt;
  
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="2dp"&gt;
  
  
          &lt;TextView
              android:id="@+id/itemTitle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:layout_centerVertical="true"
              android:layout_gravity="center_vertical"
              android:layout_toLeftOf="@+id/btnMore"
              android:text="Sample title"
              android:textColor="@android:color/black"
              android:textSize="18sp" /&gt;
  
          &lt;Button
              android:id="@+id/btnMore"
              android:layout_width="wrap_content"
              android:layout_height="42dp"
              android:layout_alignParentEnd="true"
              android:layout_alignParentRight="true"
              android:layout_centerVertical="true"
              android:theme="@style/MyButton"
              android:text="more"
              android:textColor="#FFF" /&gt;
  
  
      &lt;/RelativeLayout&gt;
  
      &lt;android.support.v7.widget.RecyclerView
          android:id="@+id/recycler_view_list"
          android:layout_width="match_parent"
          android:layout_height="160dp"
          android:layout_gravity="center_vertical"
          android:orientation="horizontal" /&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>list_single_card.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      app:cardCornerRadius="5dp"
      app:cardUseCompatPadding="true"
      &gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="0dp"
          android:background="?android:selectableItemBackground"
          android:orientation="vertical"&gt;
  
          &lt;ImageView
              android:id="@+id/itemImage"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:layout_gravity="center_horizontal"
              android:scaleType="fitCenter"
              android:src="@drawable/android" /&gt;
  
  
          &lt;TextView
              android:id="@+id/tvTitle"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@id/itemImage"
              android:gravity="center"
              android:padding="5dp"
              android:text="Sample title"
              android:textColor="@android:color/black"
              android:textSize="18sp" /&gt;
  
  
      &lt;/LinearLayout&gt;
  
  &lt;/android.support.v7.widget.CardView&gt;
  </code></pre>
  
  <p>In AndroidManifest.xml You would then need to add the following permissions:</p>
  
  <pre><code>    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  </code></pre>
  
  <p>Please help.</p>