TabLayout: How to load all the tabs or only swipe refresh occur

限于喜欢 提交于 2020-03-22 05:50:17

问题


I have an activity and i implement the android TabLayout with recyclerview. I implemented three fragments for the three tabs of the TabLayout.

The default load behavior of the TabLayout it will load and cache the neighbor tab but not the one that already loaded.

let's say Fragment A, B and C correspond to tab 1, 2 and 3 respectively.

  • When users visit tab 1 ---> Fragment A and B loaded
  • When users visit tab 2 ---> Fragment C loaded
  • But when users visit tab 1 again --> Fragment A load again.

What I want is when the activity start, I want all the three tabs load only once, then I can load the tab through refresh mechanism such as the swipe refresh.

Are there any way I can change the load behavior of the TabLayout?

Thanks,


回答1:


The viewPager by default have .setOffscreenPageLimit(1);

This way your tab's Fragment A, B and C correspond to tab 1, 2 and 3 respectively.

When users visit tab 1, fragments A and B are loaded, C isn't loaded because only tabs adjacent to tab 1 are loaded.

When you go to tab2, no tab is reloaded because they are adjacent to tab2. Instead, if you go from tab 3 to tab 1, it will be reloaded.

So you must set viewPager.setOffscreenPageLimit(2);

This way all the tabs will be loaded only once.

But if you add more tabs you must increase this number of setOffscreenPageLimit.



来源:https://stackoverflow.com/questions/34622358/tablayout-how-to-load-all-the-tabs-or-only-swipe-refresh-occur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!