简体   繁体   English

重新创建-View Pager-FragmentPagerAdapter的片段

[英]Recreate Fragment of - View Pager - FragmentPagerAdapter

In my application I am using ViewPager from the support library- v4 在我的应用程序中,我使用支持库中的ViewPager- v4

  1. In main screen I have viewPager which got max 5 Fragment, all fragment belongs to one class ArticlePager 在主屏幕上,我有viewPager,它的max 5片段最多,所有片段都属于一个类ArticlePager

  2. Now in main screen there are list on categories, now the content of the pager is based on that selection, 现在在主屏幕上有类别列表,现在寻呼机的内容是基于该选择的,

The problem I am having is, I have used FragmentPagerAdapter which stores the Fragment and if the fragment is already exist, It will return the old Fragment without recreating it. 我遇到的问题是,我使用了FragmentPagerAdapter来存储Fragment,如果该片段已经存在,它将返回旧的Fragment而无需重新创建它。 That things runs perfectly, but Problem Occurs @ the time of orientation change. 一切运行正常,但是在方向改变时出现问题。

For instance 例如

If there are 5 View normally in every fragment For the given position, but There are also some which contains 2-3 views. 如果给定位置的每个片段中通常有5个视图,但是也有一些包含2-3个视图。 Now if I change the orientation on page No. lets say 5 which contains only 3 view inside . 现在,如果我更改页面编号的方向 ,可以说5,其中包含3个视图

So, by now in every category on Page 5 I'll got the view containing 3 view, which is not something I want. 因此,到目前为止,在第5页上的每个类别中,我将获得包含3个视图的视图,这不是我想要的。

In my application each category contains the pagination 在我的应用程序中,每个类别都包含分页

Is there any way such that i can destroy and recreate the Fragment on click of category? 有什么方法可以使我单击类别即可销毁并重新创建片段? or any other work around 或其他解决方法

Thank you 谢谢

OK thanks to open source I find my solution, FragmentPagerAdapter I have override the method instantiateItem and got the solution. 好的,感谢开源,我找到了我的解决方案FragmentPagerAdapter我重写了instantiateItem方法并得到了解决方案。

This can be easily achieved by FragmentStatePagerAdapter such that it doesn't store the fragment. 这可以通过FragmentStatePagerAdapter轻松实现,使其不存储片段。 It recreate it all the time, but I don't want that in 100's of page because of only few pages. 它一直都在重新创建它,但是我不希望在100页的页面中那样,因为只有很少的页面。

So if I understand correctly, your problem is that after rotation, the wrong set of fragments are in you ViewPager? 因此,如果我理解正确,那么您的问题是旋转后,ViewPager中的片段集是否正确?

Why don't you check the current selected tab in onResume() or onStart() of your Activity, and create/assign a new PagerAdapter for you ViewPager with the correct fragments? 为什么不检查Activity的onResume()或onStart()中当前选择的选项卡,并为ViewPager创建/分配带有正确片段的新PagerAdapter?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM