我有一个使用ActionBarSherlock和主片段内我有使用几个片段显示一个列表的不同对象ViewPager的应用程序。
机头:
public class CollectionDemoFragment extends SherlockFragment {
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;
public CollectionDemoFragment() {
setTitle(R.string.title);
setHasOptionsMenu(true);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.demo_fragment, container, false);
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
return view;
}
@Override
public void onPause() {
//This runs when the fragment goes to backstack
super.onPause();
}
@Override
public void onResume() {
//This runs when the fragment returns from backstack
super.onResume();
}
}
ViewPagerAdapter:
public class DemoCollectionPagerAdapter extends
FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
return 100;
}
@Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
每个里面这个片段,我可以创造一个新的主片段与新的列表中ViewPager显示和更换新片段的内容。
ViewPager片段:
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
Integer.toString(args.getInt(ARG_OBJECT)));
//Setup components
return rootView;
}
@Override
public void setMenuVisibility(final boolean visible) {
if (visible) {
//Do something
}
super.setMenuVisibility(visible);
}
@Override
public void onPause() {
//This should run when the fragment goes to backstack
super.onPause();
}
@Override
public void onResume() {
//This should run when the fragment returns from backstack
super.onResume();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.someComponent:
Fragment newContent = new CollectionDemoFragment();
switchContent(newContent, true);
break;
}
}
public void switchContent(Fragment newContent, boolean addToBackStack) {
if (newContent != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.content_frame, newContent);
if (addToBackStack) {
ft.addToBackStack("" + newContent.getId());
}
ft.commit();
}
}
}
直到我压回,并从返回堆栈先前的主片段返回该工程确定。
屏幕是空的(由于onCreateView不叫布局不膨胀),从在ViewPager片段生命周期方法从来不被称为或者当主片段进入返回堆栈,也不返回时。 称为在ViewPager的片段的唯一方法是setMenuVisibility(),因此,仅在有运行的代码。
任何人都知道的方式来解决这个问题,为什么出现这种情况?
如果它很重要,但我由于Android 2.3支持不知道。