我正在开发一个由view-pager组成的应用程序,而我正在刷View View,所有片段都正确加载,但这是我的问题。当我再一次用相同的片段滑动View分页器时它再次加载,但根据我的要求,之前加载的片段应该恢复。我已经尝试过以下两种方式的代码。
A)这是我的View Pager
mViewPager = (ViewPager)mMyPageFragment.findViewById(R.id.Viewpager);
myAdapter = new MyAdapter(getFragmentManager());
mViewPager.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
在适配器中,
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
System.out.println("MyAdapter="+position);
switch (position) {
case 0:
return new MyPageFirstFragment1();
case 1:
return new MyPageFirstFragment2();
case 2:
return new MyPageFirstFragment3();
default:
return null;
}
}
}
第二种方法
private static Hashtable<Integer, Fragment> fragmentItems = new Hashtable<Integer, Fragment>();
@Override
public Fragment getItem(int position) {
System.out.println("MyAdapter=" + position);
switch (position) {
case 0: {
if (!fragmentItems.containsKey(position)) {
fragmentItems.put(position, new MyPageFirstFragment1());
}
return fragmentItems.get(position);
}
case 1: {
if (!fragmentItems.containsKey(position)) {
fragmentItems.put(position, new MyPageFirstFragment2());
}
return fragmentItems.get(position);
}
case 2: {
if (!fragmentItems.containsKey(position)) {
fragmentItems.put(position, new MyPageFirstFragment3());
}
return fragmentItems.get(position);
}
case 3: {
if (!fragmentItems.containsKey(position)) {
fragmentItems.put(position, new MyPageFirstFragment4());
}
return fragmentItems.get(position);
}
default:
return null;
}
}
这是我用过的代码,但我不能保留片段。
答案 0 :(得分:1)
你不应该将你的片段保存到静态对象中..因为片段管理器会在设备配置更改期间保存有关片段的信息,并在此之后恢复片段..但是将其保存到新的Fragment对象中。而你的“旧”静态对象变得无效。我更喜欢检查片段活动onCreate()方法,如果savedInstanceState!= null,如果是我恢复视图寻呼机片段的那些标签(findFragmentByTag)并从恢复的片段创建新的适配器..例如 `
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i( LOG_TAG, "onCreate()" );
//setting view pager
mViewPager = (ViewPager) findViewById( R.id.mainActivity_viewPager );
//initializing activity
if( savedInstanceState == null ) {
Log.i( LOG_TAG, "onCreate() - savedInstanceState == null" );
//activity first creation
} else {
Log.i( LOG_TAG, "onCreate(), savedInstanceState != null, it is activity start after device reconfiguration.. " );
//recreating viewpager adapter
mViewPager.setAdapter( recreateParentFragmentListAdapter() );
}
和
`
private ParentFragmentListAdapter recreateParentFragmentListAdapter() {
List<BaseParentFragment> fragments = new ArrayList<BaseParentFragment>(3);
for( int id = 0; id < 3; id++ ) {
String fragmentTag = getFragmentTag( R.id.mainActivity_viewPager, id );//int -> long converting
BaseParentFragment fragment = (BaseParentFragment) (getSupportFragmentManager().findFragmentByTag( fragmentTag ));
Log.i( LOG_TAG, "onCreate() - finding fragment with tag {" + fragmentTag + "}, found fragment = " + (fragment == null ? "null" : fragment.toString() ) );
if( fragment == null ){//creating it
switch( id ) {
case 0:
fragment = new Tab1Fragment();
case 1:
fragment = new Tab2Fragment();
case 2:
fragment = new Tab3Fragment();
}
}
fragments.add( fragment );
}
ParentFragmentListAdapter adapter = new ParentFragmentListAdapter( getSupportFragmentManager(), fragments );
return adapter;
}
` 和
private String getFragmentTag( int viewId, long id ) {
return "android:switcher:" + viewId + ":" + id;
}
来自sdk