在使用PagerAdapter时的坑

随笔,Android 2016-04-30

坑的源头

在fragment中使用

public void onStart() {
        super.onStart();
        mAdapter = new TrackGetFragmentPagerAdapter(getChildFragmentManager());
        tabLayout = (TabLayout) mainView.findViewById(R.id.tabs);
        viewPager = (ViewPager) mainView.findViewById(R.id.vp_view);
        viewPager.setAdapter(mAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        mAdapter.notifyDataSetChanged();
    }

mAdapter = new TrackGetFragmentPagerAdapter(getChildFragmentManager()); 如果是写成getActivity().getSupportFragmentManager()时,在返回桌面,再进入应用时会报


java.lang.IllegalStateException: Observer android.support.v4.view.ViewPager$PagerObserver@4239b3e8 was not registered.

本文由 zhanghuayan 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

赏个馒头吧