I'd like to cache a fragment view. My Activity has swipeable tabs and each tab calls a different fragment. But when i swipe between tabs the transition seems a quite slow because of the destruction of the fragment view, that is rebuilded during the swipe operation. Does anyone know how can i cache the view of each fragment to prevent this issue? I work with library support v4 and api 14
我想缓存片段视图。我的活动具有可滑动的标签,每个标签调用不同的片段。但是当我在标签之间滑动时,由于片段视图的破坏,过渡看起来非常缓慢,这在刷卡操作期间被重建。有谁知道如何缓存每个片段的视图以防止这个问题?我使用库支持v4和api 14
I tried to implement a constructor for the fragments, called by the activity container of the fragments: i call the constructor, the fragments are created as variable of the activity class and then, whenever a fragment has to show itself, the activity class returns the fragment object i created before, but this doesn't improve my application a lot because the view of the fragment is destroyed anyway
我试图为片段实现一个构造函数,由片段的活动容器调用:我调用构造函数,片段被创建为活动类的变量,然后,每当片段必须显示自己时,活动类返回我之前创建的片段对象,但这并没有大大改善我的应用程序,因为无论如何片段的视图都被销毁了
1 个解决方案
#1
23
This is because internally by default the pager loads a maximum of 3 pages (fragments) at the time: the one displaying, previous and next so if you have 5 fragments this will happen while you move from first to last: (where x is a loaded fragment)
这是因为默认情况下,内部默认情况下,寻呼机最多加载3页(片段):一个显示,前一个和下一个,所以如果你有5个片段,这将在你从第一个移动到最后一个时发生:(其中x是一个加载片段)
xx000 -> xxx00 -> 0xxx0 -> 00xxx -> 000xx
xx000 - > xxx00 - > 0xxx0 - > 00xxx - > 000xx
Try using
myPager.setOffscreenPageLimit(ITEMS_COUNT-1);
This will tell the pager to keep all of them in memory and not destroy/create with every swipe (keep a close look on the memory management)
这将告诉寻呼机将所有这些保留在内存中,而不是每次扫描都要销毁/创建(仔细查看内存管理)
#1
23
This is because internally by default the pager loads a maximum of 3 pages (fragments) at the time: the one displaying, previous and next so if you have 5 fragments this will happen while you move from first to last: (where x is a loaded fragment)
这是因为默认情况下,内部默认情况下,寻呼机最多加载3页(片段):一个显示,前一个和下一个,所以如果你有5个片段,这将在你从第一个移动到最后一个时发生:(其中x是一个加载片段)
xx000 -> xxx00 -> 0xxx0 -> 00xxx -> 000xx
xx000 - > xxx00 - > 0xxx0 - > 00xxx - > 000xx
Try using
myPager.setOffscreenPageLimit(ITEMS_COUNT-1);
This will tell the pager to keep all of them in memory and not destroy/create with every swipe (keep a close look on the memory management)
这将告诉寻呼机将所有这些保留在内存中,而不是每次扫描都要销毁/创建(仔细查看内存管理)