在操作栏分页标签时重新片段的观点分页、片段、观点、操作

2023-09-07 11:26:20 作者:心软是病夺我命

我用ActionBarSherlock兼容库和操作栏的选项卡之间进行分页时遇到一个奇怪的行为。每个标签包含一个简单的片段,没什么特别的。我观察到片段的 onCreateView 方法调用过于频繁,即使没有改变屏幕方向。它看起来像某种pre-缓存。我有三个选项卡在那里,在创建活动时,在 onCreateView 只调用前两个片段。最后一个片段不创建视图,直到我向前翻页一步。从最后一个选项卡,第一个分页时,会发生同样的情况。

I use ActionBarSherlock compatibility library and experience a strange behavior when paging between tabs of Action Bar. Each tab contains a simple Fragment, nothing special. I observed that fragment's onCreateView method is called too often even though there is no screen orientation change. It looks like some kind of pre-caching. I have three tabs there, when the activity is created, the onCreateView is called only for the first two fragments. The last fragment doesn't create view until I page one step forward. The same behavior occurs when paging from the last tab to the first.

有没有人为什么发生这种情况的任何想法?我会假设一次创建所有视图,当父活动完成它的创造。我不想再创建视图,并再次,有在片段没有改变,它们是静态的。它没有任何意义,并导致分页是缓慢有点...

Has anybody any idea why this occurs? I would assume creating all views at once, when the parent activity finishes its creating. I don't want to create views again and again, there are no changes in the fragments, they are static. It has no sense and causes paging to be sluggish a bit...

推荐答案

几个小时,我发现发生的事情以后那里。 ViewPager有一个默认设置DEFAULT_OFFSCREEN_PAGES它设置(在我的情况碎片)的意见的最大数量存储在ViewPager的视图容器。这显然​​是某种资源的优化;看不见的意见可以扔掉并在需要时恢复。

After a few hours I found what's happening there. ViewPager has a default setting DEFAULT_OFFSCREEN_PAGES which sets the maximum number of views (fragments in my case) to be stored in the view container of ViewPager. It is obviously some kind of resource optimization; invisible views can be thrown away and restored when needed.

有什么话更容易通过改变 setOffscreenPageLimit(INT限制)此值二传手,我忽视了。

There is nothing easier then change this value by setOffscreenPageLimit(int limit) setter which I overlooked.