我使用以下内容选择要在我的活动中显示的片段......
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
default:
fragment = null;
break;
}
return fragment;
}
...一个空白的工作片段看起来像....
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dod, container, false);
// Do Stuff Here
return root;
}
@Override public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save some state!
}
}
我的 SectionsPagerAdapter 类型不匹配的错误如何:使用 ListFragment 时无法从 Fragment2 转换为 Fragment ,如下所示.....
public class Fragment2 extends ListFragment {
DemoAdapter adapter=null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
if (adapter==null) {
ArrayList<Integer> items=new ArrayList<Integer>();
for (int i=0;i<25;i++) { items.add(i); }
adapter=new DemoAdapter(getActivity(), items);
}
else {
adapter.startProgressAnimation();
}
setListAdapter(adapter);
}
}
我一直在寻找一种方法来解决这个问题。有人可以建议一种方法吗?如果可能的话,可以解释一下,以便我理解为什么并且可以在将来尝试避免它?
谢谢