ViewPager fragments disappear when change screen rotation(更改屏幕旋转时 ViewPager 片段消失)
问题描述
我正在开发包含 ViewPager
和 Fragment
的 android 应用程序,如下所示:
I'm working on android application that contains ViewPager
with Fragment
s, like this:
MainActivity
(MainActivityFragment
(screenSlideViewPager
(Fragment
s))),意思是:
MainActivity
(MainActivityFragment
(screenSlideViewPager
(Fragment
s))), which means:
Activity
contains Fragment
contains ViewPager
contains 片段
s
现在我的问题是当旋转设备或更改屏幕旋转时,ViewPager 中的所有片段都消失了.
Now my problem is when rotate device or change screen rotation all Fragments in ViewPager are disappear.
有解决这个问题的想法吗?
any ideas to solve this issue?
编辑 1
MainActivity.java:
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
final FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(android.R.id.content, new MainActivityFragment(), TAG);
ft.commit();
}
}
MainActivityFragment.java:
MainActivityFragment.java:
public class MainActivityFragment extends Fragment {
private ViewPager mPager = null;
private PagerAdapter mPageAdapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View reVal = inflater.inflate(R.layout.activity_main, container, false);
mPager = (ViewPager) reVal.findViewById(R.id.pagerMainContent);
mPageAdapter = new ScreenSlidePagerAdapter(getActivity().getFragmentManager());
mPager.setAdapter(mPageAdapter);
return reVal;
}
private MainGridViewFragment mainGridFragment;
private AlphabetGridViewFragment alphabetGridFragment;
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public android.app.Fragment getItem(int position) {
switch (position) {
case 1:
mainGridFragment = new MainGridViewFragment();
return mainGridFragment;
case 0:
alphabetGridFragment = new AlphabetGridViewFragment();
return alphabetGridFragment;
default:
return null;
}
}
@Override
public int getCount() {
return 2;
}
}
}
编辑 2
显然MainActivity
和MainActivityFragment
都被加载了,证明是ActionBar
,另请注意,ViewPager
也已加载,因为您仍然可以在页面之间导航(蓝灯表示寻呼机已到达最后一页)但您看不到其内容.
Obviously MainActivity
and MainActivityFragment
are loaded, and the proof is The ActionBar
,
notice also that ViewPager
is loaded too, because you still can navigate between pages (the blue light means that the pager reached to last page) but you can't see its content.
推荐答案
看起来您的主要活动正在使用 getSupportFragmentManager(),而您的 Fragment 正在使用 getFragmentManager().
It looks like your main activity is using the getSupportFragmentManager() while your fragment is using getFragmentManager().
不确定这是否值得回复,但我的评分太低,无法以其他方式回复.:)
Not sure if this is worthy of an answer post but my rating is too low to reply any other way. :)
我相信您可能还需要使用支持库扩展 FragmentActivity.
I believe you may also need to extend a FragmentActivity with the support library.
参见:https://stackoverflow.com/a/10609839/2640693
编辑 2:
public class MainActivityFragment extends FragmentActivity {
private ViewPager mPager = null;
private PagerAdapter mPageAdapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View reVal = inflater.inflate(R.layout.activity_main, container, false);
mPager = (ViewPager) reVal.findViewById(R.id.pagerMainContent);
mPageAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
mPager.setAdapter(mPageAdapter);
return reVal;
}
private MainGridViewFragment mainGridFragment;
private AlphabetGridViewFragment alphabetGridFragment;
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 1:
mainGridFragment = new MainGridViewFragment();
return (Fragment)mainGridFragment;
case 0:
alphabetGridFragment = new AlphabetGridViewFragment();
return (Fragment)alphabetGridFragment;
default:
return null;
}
}
@Override
public int getCount() {
return 2;
}
}
}
这篇关于更改屏幕旋转时 ViewPager 片段消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更改屏幕旋转时 ViewPager 片段消失
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01