问题描述
我设置了 ActionBar 标签.它由 4 个选项卡组成.一切都很好,直到我离开 TabbedFragment
并返回.
我创建这样的标签:
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);最终 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));}}
并像这样初始化适配器:
@Override公共视图 onCreateView(LayoutInflater inflater,ViewGroup 容器,Bundle savedInstanceState) {返回 inflater.inflate(R.layout.paging_tab_container, container, false);}@覆盖public void onViewCreated(View view, Bundle savedInstanceState) {super.onViewCreated(view,savedInstanceState);viewPager = (ViewPager) view.findViewById(R.id.pager);viewPager.setAdapter(new FragmentStatePagerAdapter(getFragmentManager()) {@覆盖公共片段 getItem(int position) {返回 tabs.get(position).fragment;}@覆盖公共 int getCount() {返回 tabs.size();}});viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {@覆盖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 也是如此.
But when selecting tab C it is created and loaded.返回到选项卡 A,它被重新创建.
这里可能有什么问题?
过了一会儿又遇到同样的问题,所以更新这个问题.如果您使用 FragmentStatePagerAdapter
,您应该通过 getChildFragmentManager()
而不是 getFragmentManager()
提供 FragmentManager
.请参阅问题 55068:ViewPager 在通过 backstack 返回导航时不刷新子片段>
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);
}
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?
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中的片段视图在恢复时未恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!