从今天早上开始,我一直在尝试使用 viewpager 来处理屏幕旋转,我尝试了很多方法,我阅读了几乎所有关于此问题的帖子,但我无法找到我的问题。
问题
- 当我更改方向时,它会删除 3 或 4 个页面,我必须向右滑动 3 或 4 次才能查看其他页面。从纵向更改为横向几乎每次都有效,没有页面被删除,但如果我从纵向变为横向,页面就会消失
我在一篇文章中读到,这是由于方向改变时 fragment 被破坏,我们需要使用 setRetainInstance(true);在创建里面修复这个问题,但它不起作用,我在这里遗漏了什么吗?
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
if (position <= (arraySize - 1)) {
HashMap<String, String> data = computeQuestion(questoes[randomize.get(position)]);
return PlaceholderFragment.newInstance(position + 1, data, tables[randomize.get(position)]);
} else {
return FragmentResultados.newInstance(position + 1);
}
}
@Override
public int getCount() {
return arraySize + 1; // number of pages + ending page
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
default:
return String.valueOf(position);
}
}
}
fragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
编辑
fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_exame, container, false);
mViewPager = (ViewPager) getActivity().findViewById(R.id.container);
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
return rootView;
}
结果:IllegalStateException:FragmentManager 已在执行事务
Activity
private static ViewPager mViewPager;
private static FragmentStatePagerAdapter mSectionsPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exame2);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
list
<activity
android:name=".Exame"
android:label="@string/title_activity_exame"
android:theme="@style/AppTheme"
android:configChanges="keyboardHidden|orientation|screenSize"/>
activity_xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="pt.condutorresponsavel.android.testescodigo.Exame">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<ImageView
android:id="@+id/arrowLeft"
android:background="@color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_first_page_white_24dp"
android:paddingLeft="10dp"
android:paddingTop="3dp"
android:paddingRight="10dp"
android:paddingBottom="3dp"
android:onClick="toFirst"
/>
<ImageView
android:background="@color/colorPrimary"
android:onClick="toLast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_last_page_white_24dp"
android:paddingLeft="10dp"
android:paddingTop="3dp"
android:paddingRight="10dp"
android:paddingBottom="3dp"
android:layout_alignParentRight="true"
android:id="@+id/arrowRight" />
<TextView
android:id="@+id/timeleft"
android:textColor="#ffffff"
android:background="@color/colorPrimary"
android:paddingRight="10dp"
android:paddingLeft="5dp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20:01"
android:layout_alignBottom="@+id/arrowRight"
android:layout_toLeftOf="@+id/arrowRight"
android:layout_toStartOf="@+id/arrowRight"
android:layout_alignParentTop="true" />
我将代码示例上传到 github
已修复
private void InitializeUI(){
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_exame2);
InitializeUI();
}
最佳答案
我在 list 文件中缺少 android:configChanges="orientation|screenSize"
。例如:
<activity
android:name=".MyActivity"
android:configChanges="orientation|screenSize" />
关于android - 当方向改变时处理来自 ViewPager 的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39006743/