How to use ViewPagers in Tabs?

一个人想着一个人 提交于 2019-12-13 03:58:56

问题


I'm trying to add a ViewPager for each tab to display images. For the first tab on the launch, everything is fine. The Fragments are being fetched with the help of custom FragmentStatePagerAdapter. I'm instantiating a new ViewPager object in CreateTabContent method.

The problem occurs when I click on Tab 2, after the contents of first tab are displayed. I set a new FragmentStatePagerAdapter object on the new ViewPager object. Again all the methods like getCount, getItem, getItemPosition execute properly, but the objects are do not attach to the ViewPager.

On checking the hierarchviewer, I found out that NoSaveStateFrameLayout objects are not being created.

Thanks.

Note: I've implemented notifyDataSetChanged.

The problem occurs


回答1:


I found out the solution. I checked the hierarchyviewer, again, carefully and found out that NoSaveStateFrameLayout object, which should be attached to the ViewPager for the next tab was being attached to the first ViewPager. So, I detached the ViewPager on tab 1 when next tab is clicked using this code:

FrameLayout frameLayout  = mTabHost.getTabContentView();
frameLayout.removeViewAt(index);

Now, Only one ViewPager object exists and everything is working great.



来源:https://stackoverflow.com/questions/10602787/how-to-use-viewpagers-in-tabs

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