title: Nested RecyclerViews
body: <p>I'm creating a calendar app, with each month being displayed using a RecylcerView. Inside each day of each month, there is another RecyclerView to display events on that day. I'm using the custuomLinearLayoutManager from <a href="https://stackoverflow.com/a/38513905/13199395">https://stackoverflow.com/a/38513905/13199395</a>, and I've looked at many examples. My code for the nesting is very similar to them but for some reason is not working. Any help as to why is not working would be much appreciated.</p>
  
  <p>The month view:</p>
  
  <pre><code>&lt;FrameLayout 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="match_parent"
      android:orientation="vertical"
      tools:context=".MonthViewActivity"&gt;
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:id="@+id/calView"
          android:layout_width="match_parent"
          android:layout_height="581dp"
  
          android:layout_gravity="bottom" /&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>The view for each day:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/dayView"
      android:layout_width="145px"
      android:layout_height="265px"
      android:orientation="vertical"&gt;
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:id="@+id/eventDayView"
          android:layout_width="wrap_content"
  
          android:layout_height="wrap_content"&gt;
  
      &lt;/androidx.recyclerview.widget.RecyclerView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>and the view for the inner recyclerView:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/showEvents"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;TextView
          android:id="@+id/eventName"
          android:layout_width="145px"
          android:layout_height="wrap_content" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The adapter for the main recyclerview:</p>
  
  <pre><code>public class MonthViewAdapter extends RecyclerView.Adapter&lt;MonthViewAdapter.ViewHolder&gt;  {
  
      Context context;
      ArrayList&lt;Day&gt; days;
  
      CustomLinearLayoutManager l;
  
      public MonthViewAdapter(Context context, ArrayList&lt;Day&gt; arrayList) {
  
          this.context = context;
          this.days = arrayList;
      }
  
      @NonNull
      @Override
      public MonthViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(context).inflate(R.layout.content_events, parent, false);
          return new ViewHolder(view, this.mOnDayClickListener);
      }
  
      @Override
      public void onBindViewHolder(MonthViewAdapter.ViewHolder holder, int position) {
  
          l = new CustomLinearLayoutManager(context, LinearLayout.VERTICAL, false);
          ViewGroup.LayoutParams params=holder.r.getLayoutParams();
          params.height=100;
          holder.r.setLayoutParams(params);
          holder.r.setLayoutManager(l);
          EventViewAdapter adapter = new EventViewAdapter(context, days.get(position).getEvents(), this.mOnDayClickListener);
  
          holder.r.setAdapter(adapter);
          holder.t.setText(days.get(position).getMonthDayNumber());
  
      }
  
      @Override
      public int getItemCount() {
          return days.size();
      }
      public class ViewHolder extends RecyclerView.ViewHolder  {
          RecyclerView r;
          TextView t;
          FrameLayout f;
          LinearLayout l;
  
          public ViewHolder(View itemView) {
              super(itemView);
              l = itemView.findViewById(R.id.dayView);
              t = itemView.findViewById(R.id.dayNum);
              r  = itemView.findViewById(R.id.eventDayView);
  
  
          } 
      }
  }
  </code></pre>
  
  <p>The adapter for the inner RecyclerView:</p>
  
  <pre><code>public class EventViewAdapter extends RecyclerView.Adapter&lt;EventViewAdapter.ViewHolder&gt; {
  
      Context context;
      ArrayList&lt;Event&gt; events;
  
  
      public EventViewAdapter (Context context, ArrayList&lt;Event&gt; arrayList) {
          this.context = context;
          this.events = arrayList;
  
      }
  
      @Override
      public EventViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(context).inflate(R.layout.content_single_event, parent, false);
          return new ViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(EventViewAdapter.ViewHolder holder, int position) {
          Log.d("event", Integer.toString(events.size()));
          holder.eventName.setText(events.get(position).getName());
  
      }
  
      @Override
      public int getItemCount() {
          return events.size();
      }
  
      public class ViewHolder extends RecyclerView.ViewHolder {
          TextView eventName;
  
          LinearLayout linearLayout;
  
          public ViewHolder(View itemView) {
              super(itemView);
              eventName = itemView.findViewById(R.id.eventName);
              linearLayout = itemView.findViewById(R.id.showEvents);
  
          }
      }
  }
  </code></pre>
  
  <p>The custom linear layout manager: <a href="https://stackoverflow.com/a/38513905/13199395">https://stackoverflow.com/a/38513905/13199395</a></p>
  
  <p>And the activity</p>
  
  <pre><code>public class MonthViewActivity extends AppCompatActivity{
  
          RecyclerView month;
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_month_view);
  
              month.setHasFixedSize(true);
              month.addItemDecoration(new DividerItemDecoration(this,
                      DividerItemDecoration.HORIZONTAL));
              month.addItemDecoration(new DividerItemDecoration(this,
                      DividerItemDecoration.VERTICAL));
  
              GridLayoutManager g = new GridLayoutManager(this, 7);
  
              month.setLayoutManager(g);
  
              ArrayList&lt;Day&gt; day = new ArrayList&lt;&gt;();
              LocalDate dd = LocalDate.now();
              Day d1 = new Day(dd);
              Day d2 = new Day(dd.plusDays(1));
              Day d3 = new Day(dd.plusDays(2));
              d1.addEvent(new Event("a"));
              day.add(d1);
              adapter = new MonthViewAdapter(this, day, this);
              month.setAdapter(adapter);
          }
      }
  </code></pre>
  
  <p>Each element of the list that I pass into the month view adapter has a list of events that are then passed into the event adapter. However, each day in the grid only shows one of the events, it does not show all of the events in the event list on each day. Why is this happening and how can I fix it? I am getting an error (java.lang.IndexOutOfBoundsException: Invalid item position) on </p>
  
  <pre><code>View view = recycler.getViewForPosition(position);
  </code></pre>
  
  <p>in the custom layout manager, but the program does not crash.</p>