问题描述
我有一个使用 ActionBarSherlock 的应用程序,在主片段中我有一个 ViewPager,它使用多个片段来显示列表的不同对象.
I have an application that uses ActionBarSherlock and inside the main fragment I have a ViewPager which uses several fragments to display different objects of a list.
主要片段:
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 中,并用这个新片段替换内容.
Inside each of this fragments I can create a new Main fragment with a new list to display in the ViewPager and replace the content with this new fragment.
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();
}
}
}
这可以正常工作,直到我按下返回并且前一个主要片段从后台堆栈返回.
This works ok, until I press back and the previous main fragment returns from the backstack.
屏幕是空的(因为没有调用 onCreateView 布局没有膨胀),ViewPager 中片段的生命周期方法在主片段进入 backstack 时或返回时都不会被调用.ViewPager 的片段中调用的唯一方法是 setMenuVisibility(),因此只有其中的代码运行.
The screen is empty (since onCreateView is not called the layout is not inflated), the lifecycle methods from the fragments in the ViewPager are never called either when the main fragment enters the backstack, nor when it returns . The only method called in the fragments of the ViewPager is the setMenuVisibility() so, only the code in there runs.
任何人都知道解决此问题的方法以及为什么会发生这种情况?
Anyone knows a way to fix this and why this happens?
不确定是否重要,但我必须从 android 2.3 开始支持.
Not sure if it matters but I have to support since android 2.3.
推荐答案
当您创建视图适配器时,您必须将片段子管理器作为参数传递 - getChildFragmentManager()
而不是 getFragmentManager()
.
When you are creating the view adapter You have to pass the fragment child manager as argument - getChildFragmentManager()
instead of getFragmentManager()
.
而不是,
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getFragmentManager());
你应该使用,
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getChildFragmentManager());
这篇关于带有 ViewPager 的 ActionBarSherlock 在返回堆栈和返回堆栈时不调用 ViewPager 片段生命周期方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!