1

I have a ViewPager with three tabs. In each of the tabs there is a list of movies.

I made it so there is one fragment that contains the ListView and every time the user chooses a tab, a static reference in the MainActivity changes, causing the adapter of the list view to refresh with other types of data.

So it's designed like this:

MovieListFragment: a fragment containing a list view and initialising it.
PagerAdapter: the adapter for the view pager. In the getItem method, it simply returns a new instance of the MovieListFragment.
MoviesFragment: This is the fragment that holds everything together. It initialises the tabs and the ViewPager and connects them. It also adds a listener for tab select, changing the dataType for the list view accordingly.

By adding logs, I found that the dataType does change correctly.
However, the onCreateView in the MovieListFragment is not called every time a tab is selected. Thus, the adapter is not refreshing so the list shown is not with the correct dataType.

How do I create the fragment every time a tab is selected, so I can correctly set the dataType for the list view in that specific tab?

Gofilord
  • 6,239
  • 4
  • 27
  • 43
  • Are you using FragmentPagerAdapter? It reuses the fragment so `onCreateView` will not be called every time, that's expected. – Vucko Jul 10 '16 at 12:18
  • @Vucko I am. Yes, that's exactly the problem. How do I get around it? – Gofilord Jul 10 '16 at 12:19
  • Just have your Custom adapter extend FragmentStatePagerAdapter. – Vucko Jul 10 '16 at 12:20
  • @Vucko Thanks for the suggestion, but it doesn't change anything. – Gofilord Jul 10 '16 at 12:37
  • I think this is what you are looking for http://stackoverflow.com/questions/10073214/viewpager-setoffscreenpagelimit0-doesnt-work-as-expected/25676323#25676323 – Dimitris Konomis Jul 10 '16 at 13:00
  • @DimitrisKonomis That's great, but it seems that the `setUserVisibleHint` is faster than the dataType updating on select of the tab. I mean, it fires the function before I can set the correct dataType according to the tab selection. – Gofilord Jul 10 '16 at 13:09
  • You can use a handler for that. Also try to use mViewPager.setOffscreenPageLimit(0); but i don't know if its gone work cause i can't use my laptop right now and i can't test it – Dimitris Konomis Jul 10 '16 at 13:15
  • Thanks! Ended up using a handler with a 10ms delay. It works now, but it feels like a dirty solution. – Gofilord Jul 10 '16 at 15:10

0 Answers0