调用viewPager.setAdapter

时间:2018-01-13 10:06:54

标签: java android android-viewpager

当我致电mViewPager.setAdapter时,我的应用停止响应。一旦发生这种情况,LOGCAT会多次输出以下消息:

  

"背景粘性并发标记扫描GC释放253980(5MB)   AllocSpace对象,16个(256KB)LOS对象,8%空闲,44MB / 48MB,暂停   5.693ms总计37.544ms"

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View inflatedView = inflater.inflate(R.layout.fragment_options, container, false);
        mViewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager_options);
        mViewPager.setAdapter(new Adapter(getChildFragmentManager()));

        return inflatedView;
    }

    private class Adapter extends FragmentPagerAdapter {

        private static final int TABS = 3;

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0: return new OptionsFragment();
                default: return new OptionsFragment();
            }
        }

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

1 个答案:

答案 0 :(得分:0)

@Override
        public Fragment getItem(int position) {
            switch (position){
                case 0: return new OptionsFragment();
                default: return new OptionsFragment();
            }
        }

为什么OptionsFragment包含OptionsFragment。它的循环视图