I have a snippet containing a RecyclerView to display events for a given day. I use ViewPager to split fragments for several days; Fragment for Saturday events and a fragment for Sunday events.
However, it seems that both fragments refer to the same RecyclerView and / or Adapter, as this is only the last tab (in this case, Sunday) whose events are displayed.
In my particular case, Saturday has two events, and on Sunday there are no events. Both fragments have empty RecyclerViews. To confirm my theory that it was caused by the last tab, I switched the date. This led to both RecyclerViews having two events (from Saturday).
Here is the appropriate code for the individual fragments:
public class EventListFragment extends Fragment{ private EventAdapter mEventAdapter; private static final String DATE_ARG = "eventDate"; public static EventListFragment newInstance(LocalDate date){ EventListFragment eventListFragment = new EventListFragment(); Bundle args = new Bundle(); args.putSerializable(DATE_ARG, date); eventListFragment.setArguments(args); return eventListFragment; } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_event_list, container, false);
getEvents() is just a private function to return events for a given date. I used a debugger as well as unit tests to make sure it was working correctly. The debugger shows that it is typing the correct list for each fragment, but, as I explained, they are not displayed correctly.
Here is the appropriate code for the parent fragment:
public class EventFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_event, container, false);
Like the previous one, getEventDates() simply carries the dates on which events occur. For testing at the moment, I hard code the returned list of dates, since we do not yet have our database. I pulled out this method because I want the application to be able to function again in 2016, which may have different dates:
private List<LocalDate> getEventDates(){ List<LocalDate> eventDates = new ArrayList<>(); eventDates.add(new LocalDate(2015, 10, 17)); eventDates.add(new LocalDate(2015, 10, 18)); return eventDates; }
The last bit of the corresponding code for the FragmentStatePagerAdapter that I use for my ViewPager:
public class EventFragmentAdapter extends FragmentStatePagerAdapter { private List<LocalDate> mEventDates; public EventFragmentAdapter(FragmentManager fragmentManager, List<LocalDate> eventDates){ super(fragmentManager); this.mEventDates = eventDates; } @Override public Fragment getItem(int i) { return EventListFragment.newInstance(mEventDates.get(i)); } @Override public int getCount() { return mEventDates.size(); } @Override public CharSequence getPageTitle(int position) { return mEventDates.get(position).dayOfWeek().getAsText(); } }
Any ideas why both lists are always the same and based on the last tab in ViewPager? I assume that somehow they refer to the same RecyclerView or the same RecyclerViewAdapter, but I don't have any static fields for them, so I'm not sure how this happens.