ViewPager片段不在onCreate中启动

时间:2014-04-10 19:24:25

标签: android android-fragments android-viewpager

我似乎在更新我在ViewPager中使用的片段时出现问题,无论我是否尝试使用onCreate(),onCreateView()或onResume()。以下是我在MainFragment中设置ViewPager的方法:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View rootView = inflater.inflate(R.layout.fragment_main_screen, container, false);

        mPager = (ViewPager)rootView.findViewById(R.id.pager);
        mPager.setOffscreenPageLimit(2); // So all 3 pages are loaded at once.
        mAdapter = new JournalPagerAdapter(getActivity().getSupportFragmentManager(), this);
        mPager.setAdapter(mAdapter);

        // Add bar graph to view
        mGraphLayout = (LinearLayout) rootView.findViewById(R.id.journalGraph);
        updateGraph();
        mGraphLayout.addView(mGraphView);

        mPainFrag = (PainFragment)mAdapter.getRegisteredFragment(0);
        // Null pointer here, but if I put the action in a button listener, it works.
        mPainFrag.setScale(mEntry.getPain());
...

我通过FragmentPagerAdapter中的一些重写方法访问片段:

public class JournalPagerAdapter extends FragmentPagerAdapter {

    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
    private View.OnClickListener mOnClickListener;


    public JournalPagerAdapter(FragmentManager mgr, View.OnClickListener onClickListener) {
        super(mgr);
        mOnClickListener = onClickListener;
    }
    @Override
    public Fragment getItem(int pos) {
        switch(pos) {

            case 0: return PainFragment.newInstance("PainFragment", mOnClickListener);
            case 1: return StressFragment.newInstance("StressFragment", mOnClickListener);
            case 2: return SleepFragment.newInstance("SleepFragment", mOnClickListener);
            default: return PainFragment.newInstance("PainFragment", mOnClickListener);
        }
    }

    @Override
    public int getCount() {
        return 3;
    }

    /* Thanks to Streets of Boston (http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager)
    */

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.v("rx", "itemInstantiated");
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }

我似乎无法在设置适配器后弄清楚为什么片段为空,但如果我将片段更新代码放在点击事件中,我就没有问题。

2 个答案:

答案 0 :(得分:7)

我会尝试向ViewPager添加布局侦听器,以便在发布视图时收到通知。

当您创建ViewPager调用mPager.getViewTreeObserver().addOnGlobalLayoutListener()并传递实施OnGlobalLayoutListener的内容时。

在回调方法中,进行片段更新。确保在回调中调用mPager.getViewTreeObserver().removeGlobalOnLayoutListener(this),否则将多次调用回调。

答案 1 :(得分:0)

您可以在PagerAdapter的构造函数中实例化您的片段,只需让getItem返回它们而不是实例化它们。