我有一个项目,它有一个MainActivity,它托管一个片段,通过Viewpager托管另外两个片段.它在首次启动时看起来很好,但是当我旋转时,Viewpager中的两个片段都会丢失.我已经尝试将getFragmentManager更改为getChildFragmentManager,但我会得到一个非法状态异常:无法保留嵌套在其他片段中的fragements.
MainActivity.java:
import android.app.ActionBar; import android.os.Bundle; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity; public class MainActivity extends SlidingFragmentActivity { @TargetApi(11) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager() .beginTransaction() .replace(R.id.mainContentFrame,new WorkOutsContainerFrag()) .commit(); setBehindContentView(R.layout.menu); ActionBar actionBar = getActionBar(); populate(); SlidingMenu menu = getSlidingMenu(); menu.setMode(SlidingMenu.LEFT); menu.setShadowWidthRes(R.dimen.shadow_width); menu.setShadowDrawable(R.drawable.shadow); menu.setBehindWidthRes(R.dimen.slidingmenu_offset); menu.setFadeDegree(0.35f); menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); menu.setSlidingEnabled(false); setSlidingActionBarEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: toggle(); return true; default: return super.onOptionsItemSelected(item); } } private void populate() { ListView lv = (ListView) findViewById(R.id.listView1); String[] values = new String[] { "Workouts","Information","Ladders"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,values); lv.setAdapter(adapter); }
}
WorkoutsContainerFrag.java:
import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.astuetz.PagerSlidingTabStrip; public class WorkOutsContainerFrag extends Fragment { private PagerSlidingTabStrip tabs; private ViewPager pager; private WorkoutsPagerAdapter adapter; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.workoutscontainer,container,false); setRetainInstance(true); tabs = (PagerSlidingTabStrip) view.findViewById(R.id.workoutstabs); pager = (ViewPager) view.findViewById(R.id.workoutspager); adapter = new WorkoutsPagerAdapter(getFragmentManager()); pager.setAdapter(adapter); final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4,getResources() .getDisplayMetrics()); pager.setPageMargin(pageMargin); tabs.setViewPager(pager); return view;
}
private GymWorkoutsFragment gymWorkoutsFragment; private OCWorkoutsFragment ocWorkoutsFragment; public class WorkoutsPagerAdapter extends FragmentPagerAdapter { private final String[] TITLES = {"Gym Workouts","OC Workouts"}; public WorkoutsPagerAdapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int position) { return TITLES[position]; } @Override public int getCount() { return TITLES.length; } @Override public Fragment getItem(int position) { switch (position) { case 0: gymWorkoutsFragment = new GymWorkoutsFragment(); return (Fragment)gymWorkoutsFragment; case 1: ocWorkoutsFragment = new OCWorkoutsFragment(); return (Fragment)ocWorkoutsFragment; } return null; }
}
}
解决方法
答案实际上非常简单,就是用FragmentStatePagerAdapter替换FragmentPagerAdapter