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 https://stackoverflow.com/a/38513905/13199395, 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.
The month view:
<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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/calView"
android:layout_width="match_parent"
android:layout_height="581dp"
android:layout_gravity="bottom" />
</FrameLayout>
The view for each day:
<?xml version="1.0" encoding="utf-8"?>
<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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/eventDayView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
and the view for the inner recyclerView:
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:id="@+id/eventName"
android:layout_width="145px"
android:layout_height="wrap_content" />
</LinearLayout>
The adapter for the main recyclerview:
public class MonthViewAdapter extends RecyclerView.Adapter<MonthViewAdapter.ViewHolder> {
Context context;
ArrayList<Day> days;
CustomLinearLayoutManager l;
public MonthViewAdapter(Context context, ArrayList<Day> 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);
}
}
}
The adapter for the inner RecyclerView:
public class EventViewAdapter extends RecyclerView.Adapter<EventViewAdapter.ViewHolder> {
Context context;
ArrayList<Event> events;
public EventViewAdapter (Context context, ArrayList<Event> 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);
}
}
}
The custom linear layout manager: https://stackoverflow.com/a/38513905/13199395
And the activity
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<Day> day = new ArrayList<>();
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);
}
}
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
View view = recycler.getViewForPosition(position);
in the custom layout manager, but the program does not crash.