5

我有一个使用 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);
    }
}

在每个片段中,我可以创建一个带有新列表的新 Main 片段以显示在 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 中片段的生命周期方法在主片段进入 backstack 时或返回时都不会被调用。ViewPager 的片段中调用的唯一方法是 setMenuVisibility(),因此只有其中的代码运行。

任何人都知道解决此问题的方法以及为什么会发生这种情况?

不知道这是否重要,但我必须从 android 2.3 开始支持。

4

1 回答 1

18

创建视图适配器时,您必须将片段子管理器作为参数传递 -getChildFragmentManager()而不是getFragmentManager().

代替,

 mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getFragmentManager());

你应该使用,

 mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getChildFragmentManager());
于 2013-02-05T13:48:24.880 回答