title: Android TabLayout with two tabs and a recyclerview in each scrolls to top when switching
body: <p>I have an Activity with a TabLayout and a Viewpager that contains two tabs. These tabs are one Fragment that is reused which contains a Recyclerview. So each Fragment/Tab contains a recyclerview with a bunch of item.</p>
  
  <p>When switching between these tabs the scroll state isn't retained when I go back to the previous tab. </p>
  
  <p>What am I missing?</p>
  
  <p>Activity:</p>
  
  <pre><code>public class EventsActivity extends AbstractTracActivity implements OnRowClickListener {
  
      @BindView(R.id.toolbar)
      Toolbar toolbar;
  
      @BindView(R.id.tabs)
      TabLayout tabLayout;
  
      @BindView(R.id.viewpager)
      ViewPager viewPager;
  
      private final String TAG = EventsActivity.class.getSimpleName();
  
      private static Context context;
      private SearchView searchView;
      private static EventListFragment eventsFragment, clubsFragment;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          setTheme(R.style.TracTracAppTheme);
          super.onCreate(savedInstanceState);
          setContentView(R.layout.events);
          ButterKnife.bind(this);
  
          context = this;
  
          setSupportActionBar(toolbar);
          if (getSupportActionBar() != null) {
              getSupportActionBar().setLogo(R.drawable.tractrac_logo);
              getSupportActionBar().setDisplayShowTitleEnabled(false);
          }
  
          eventsFragment = EventListFragment.newInstance(EventListFragment.ListType.EVENTS);
          clubsFragment = EventListFragment.newInstance(EventListFragment.ListType.CLUBS);
  
          TabLayout.Tab events = tabLayout.newTab();
          TabLayout.Tab clubs = tabLayout.newTab();
  
          tabLayout.addTab(events, 0);
          tabLayout.addTab(clubs, 1);
  
          tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.accent));
          tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.accent));
  
          ViewPagerAdapter adapter = new ViewPagerAdapter (getSupportFragmentManager());
          viewPager.setAdapter(adapter);
          tabLayout.setupWithViewPager(viewPager);
  
          viewPager.setOffscreenPageLimit(2);
          viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
              @Override
              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
  
              @Override
              public void onPageSelected(int position) { }
  
              @Override
              public void onPageScrollStateChanged(int state) {
                  searchView.setQuery("", true);
                  searchView.clearFocus();
                  searchView.setIconified(true);
                  eventsFragment.filterList(null);
                  clubsFragment.filterList(null);
              }
          });
      }
  
      ...
  
      public static class ViewPagerAdapter extends FragmentPagerAdapter {
  
          public ViewPagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int position) {
              switch (position) {
                  case 1:
                      return clubsFragment;
                  default:
                      return eventsFragment;
              }
          }
  
          @Override
          public int getCount() {
              return 2;
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              switch (position) {
                  case 1:
                      return context.getResources().getString(R.string.section_clubs);
                  default:
                      return context.getResources().getString(R.string.section_events);
              }
          }
      }
  }
  </code></pre>
  
  <p>Fragment:</p>
  
  <pre><code>public class EventListFragment extends Fragment implements EventsFragmentPresenter.ViewInterface {
  
      enum ListType { EVENTS, CLUBS }
  
      private EventsFragmentPresenter presenter;
      private ListType listType;
      private List&lt;Event&gt; events;
      private RecyclerView recyclerView;
  
      public static EventListFragment newInstance(ListType listType) {
          EventListFragment f = new EventListFragment();
          Bundle args = new Bundle();
          args.putSerializable("listType", listType);
          f.setArguments(args);
          return f;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(com.tractrac.androidlib.R.layout.base_list_fragment, container, false);
      }
  
      @Override
      public void onViewCreated(View view, Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          recyclerView = (RecyclerView) view.findViewById(com.tractrac.androidlib.R.id.recycler_view);
  
          switch (getListType()) {
              case EVENTS:
                  getPresenter().getEvents();
                  break;
              case CLUBS:
                  getPresenter().getClubEvents();
                  break;
          }
      }
  
      private EventsFragmentPresenter getPresenter() {
          if (presenter == null) {
              presenter = new EventsFragmentPresenter();
              presenter.initialize(this);
          }
          return presenter;
      }
  
      private ListType getListType() {
          if (listType == null) {
              listType = (ListType) getArguments().getSerializable("listType");
          }
          return listType;
      }
  
      @Override
      public void listReady(List&lt;Event&gt; events) {
          EventsRecyclerAdapter adapter = new EventsRecyclerAdapter(events, (EventsActivity) getActivity());
          RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
          recyclerView.setLayoutManager(mLayoutManager);
          recyclerView.setItemAnimator(new DefaultItemAnimator());
          recyclerView.setAdapter(adapter);
  
          if (getListType() == ListType.EVENTS) {
              mLayoutManager.scrollToPosition(getPresenter().getCurrentDayEventIndex(events));
          }
          this.events = events;
          ((EventsActivity) getActivity()).hideSpinner();
      }
  
      public void filterList(String filterBy) {
          if (TextUtils.isEmpty(filterBy)) {
              EventsRecyclerAdapter adapter = new EventsRecyclerAdapter(events, (EventsActivity) getActivity());
              recyclerView.setAdapter(adapter);
              return;
          }
  
          List&lt;Event&gt; filteredEvents = new ArrayList&lt;&gt;();
          for (Event event : events) {
              if (event.getName().toLowerCase().contains(filterBy) || event.getLocation().toLowerCase().contains(filterBy))
                  filteredEvents.add(event);
          }
          EventsRecyclerAdapter adapter = new EventsRecyclerAdapter(filteredEvents, (EventsActivity) getActivity());
          recyclerView.setAdapter(adapter);
      }
  }
  </code></pre>