本文介绍了在ViewPager片段视图恢复时未恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有动作条选项卡设置。它由4个选项卡。一切都很好,直到我从 TabbedFragment 导航离开并返回了。

I have ActionBar Tabs setup. It consists of 4 tabs. Everything is fine until I navigate away from TabbedFragment and returning back.

我创建的标签是这样的:

I create tabs like this:

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ActionBar actionBar = getActionBar();

        tabs = Lists.newArrayList();
        tabs.add(new TabDefinition<>("Tab 1"));
        tabs.add(new TabDefinition<>("Tab 2"));
        tabs.add(new TabDefinition<>("Tab 3"));
        tabs.add(new TabDefinition<>("Tab 4"));


        for (TabDefinition tab : tabs) {
            actionBar.addTab(actionBar.newTab()
                .setText(tab.text)
                .setTag(tab.tag)
                .setTabListener(this));
        }
    }

和初始化适配器是这样的:

And initialize adapter like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.paging_tab_container, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    viewPager = (ViewPager) view.findViewById(R.id.pager);

    viewPager.setAdapter(new FragmentStatePagerAdapter(getFragmentManager()) {

        @Override
        public Fragment getItem(int position) {
            return tabs.get(position).fragment;
        }

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

    viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            getActionBar().setSelectedNavigationItem(position);
        }
    });

    viewPager.setCurrentItem(getActionBar().getSelectedNavigationIndex(), true);
}

当返回回 TabbedFragment 选中的标签和1来表示就没有任何内容。只是空视图。但是,如果我选择当前+ 2片段内容加载。然后返回到第一个片段内容被重新装载。
比如我有A,B,C,D选项卡。临走 TabbedFragment 我已经选择标签A.当返回到 TabbedFragment 我依然在选项卡A,但它是空的。那么,标签B.
但是,在选择标签C下它被创建并加载。回到标签一个是重新创建。

When returning back to TabbedFragment selected tab and 1 next to it would not have any content. Just empty view. But if I select current + 2 fragment content is loaded. And then returning to that first fragment content is reloaded.
For example I have A, B, C, D tabs. Before leaving TabbedFragment I had selected tab A.When returning to TabbedFragment I still am at tab A, but it's empty. So is tab B.
But when selecting tab C it is created and loaded. Returning to tab A it is recreated.

这可能是什么问题就在这里?

What could be the problem here?

推荐答案

过了一会遇到同样的问题又来了,所以更新这个问题。如果你使用 FragmentStatePagerAdapter 你应该提供 FragmentManager 通过 getChildFragmentManager()>而不是。请参见问题55068:ViewPager不刷新子片段时,通过backstack

After a while ran into the same problem again, so updating this question.If you're using FragmentStatePagerAdapter you should provide FragmentManager via getChildFragmentManager() instead of getFragmentManager(). See Issue 55068: ViewPager doesn't refresh child fragments when back navigation via backstack

这篇关于在ViewPager片段视图恢复时未恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!