简体   繁体   English

在ViewPager中旋转后,选项卡内容消失了

[英]Tab content disappeared after rotation inside ViewPager

I put a FragmentTabHost into a ViewPager, but the tab content has gone after I rotate the device. 我将FragmentTabHost放入ViewPager中,但是旋转设备后选项卡上的内容消失了。 The tab content will come back if I click on another tab to refresh content. 如果我单击另一个选项卡以刷新内容,则选项卡内容将返回。 Is there anything wrong in my code? 我的代码有什么问题吗? Please help me know. 请帮我知道。

public class MyActivity extends FragmentActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LayoutInflater inflater = getLayoutInflater();

        View tabView = inflater.inflate(R.layout.tab, null);
        FragmentTabHost tabHost = (FragmentTabHost) tabView.findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1"), TabContent1Fragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2"), TabContent2Fragment.class, null);

        View page2 = inflater.inflate(R.layout.page2, null);
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        List<View> list = new ArrayList<View>();
        list.add(page2);
        list.add(tabView);

        viewPager.setAdapter(new DemoPagerAdapter(list));
    }
}


public class TabContent1Fragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.tab_content_1, null);
    }
}

public class DemoPagerAdapter extends PagerAdapter
{
    private List<View> mModeViewPageList;

    public DemoPagerAdapter(List<View> modeViewPageList)
    {
       this.mModeViewPageList = modeViewPageList;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        container.addView(mModeViewPageList.get(position), 0);
        return mModeViewPageList.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object)
    {
        container.removeView(mModeViewPageList.get(position));
    }

    @Override
    public int getCount()
    {
        return mModeViewPageList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object o)
    {
        return view == o;
    }
}

Thanks to my friend, finally I found the reason was that I used PagerAdapter . 多亏了我的朋友,我终于找到原因是我使用了PagerAdapter

When device is rotated, all views will be recreated. 旋转设备时,将重新创建所有视图。 But without extra codes for fragment management, PagerAdapter is not able to mange the state of the fragments inside views (tab content fragments in realtabcontent in my case) and the child fragment of a view will disappear after rotation. 但是,如果没有用于片段管理的额外代码, PagerAdapter无法管理视图内片段的状态(在我的情况下,标签内容片段为realtabcontent),并且视图的子片段在旋转后将消失。

So I should change all views for ViewPager into fragments and use FragmentPagerAdapter instead. 因此,我应该将ViewPager所有视图ViewPager为片段,并改用FragmentPagerAdapter

One more thing, it should be used getChildFragmentManager() from Fragment in onCreateView instead of getSupportFragmentManger() from FragmentActivity to manage the child fragments in a fragment. 还有一件事,应该使用onCreateView中Fragment中的getChildFragmentManager()而不是FragmentActivity中的getSupportFragmentManger()来管理片段中的子片段。

I wanted to point out an important point in Light's answer. 我想指出Light的答案中的重要一点。 If you convert an Activity to a Fragment then you may have been using getSupportFragmentManager . 如果将Activity转换为Fragment则可能一直在使用getSupportFragmentManager When converting it, you may have changed it to getActivity().getSupportFragmentManager() . 转换时,您可能已将其更改为getActivity().getSupportFragmentManager() It will seemingly work until you leave and return to the fragment, and you'll find some fragments missing until you scroll the pager. 在您离开并返回片段之前,它似乎可以正常工作,直到滚动寻呼机时,您会发现一些片段丢失。

The solution is to change getActivity().getSupportFragmentManager() to getChildFragmentManager() . 解决方案是将getActivity().getSupportFragmentManager()更改为getChildFragmentManager()

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

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