简体   繁体   English

FragmentPagerAdapter未调用getItem()方法

[英]FragmentPagerAdapter not called getItem() method

I am creating custom views having Viewpager and custom fragment classes. 我正在创建具有Viewpager和自定义片段类的自定义视图。 The issue is my getItem(int position) not called. 问题是我的getItem(int position)没有被调用。 even this is working in another pagerview . 即使这正在另一个pagerview工作。 Please let me know what is wrong with my code.Thanks!! 请让我知道我的代码有什么问题。谢谢!!

 private void initView(Context context) {
                this.context = context;
                LayoutInflater inflater = LayoutInflater.from(context);
                rootView = inflater.inflate(R.layout.internal_training_view, this, true);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                ((HFTrainingActivityDashBord)context).setSupportActionBar(toolbar);
                ((HFTrainingActivityDashBord)context).setSupportActionBar(toolbar);
                mSectionsPagerAdapterInternal = new SectionsPagerAdapter(((HFTrainingActivityDashBord)context).getSupportFragmentManager());
                mViewPagerInternal = (CustomViewPager) findViewById(R.id.pager_hf_training);
                mViewPagerInternal.setOffscreenPageLimit(3);
                mViewPagerInternal.setAdapter(mSectionsPagerAdapterInternal);
                TabLayout tabs_hf = (TabLayout) findViewById(R.id.tabs_hf);
                tabs_hf.setupWithViewPager(mViewPagerInternal);

              }        
        public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

                @Override
                public Fragment getItem(int position) {                       

                    return new IternalTrainingDashBoardFragment(position,context);
                }

                @Override
                public int getCount() {
                    // Show 4 total pages.
                    return 4;
                }

                @Override
                public CharSequence getPageTitle(int position) {
                    switch (position) {
                        case 0:
                            return "All";
                        case 1:
                            return "InProgress";
                        case 2:
                            return "Overdue";
                        case 3:
                            return "Not Started";


                    }
                    return null;
                }

            }

I think the issue comes with how you are returning the fragments in the getItem() method. 我认为问题在于您如何在getItem()方法中返回fragments Use the static newInstance() method in your fragment to create the fragment. 在片段中使用static newInstance()方法创建片段。 Not sure if you are to call a fragment using its constructor, rather the newInstance factory method Here is an example. 不知道是否要使用片段的构造函数来调用片段,而是使用newInstance工厂方法来调用。这是一个示例。

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            //Note I am using the Fragments static newInstance method(). You arent supposed to call a fragment using new
            return DetailInfoFragment.newInstance(someEntity);
        case 1:
            return DetailContentFragment.newInstance(someEntity);
        case 2:
            return DetailExtraFragment.newInstance(someEntity);
        default:
            return DetailInfoFragment.newInstance(someEntity);
    }
}

Here is a sample newInstance in my DetailInfoFragment class 这是我的DetailInfoFragment类中的示例newInstance

public static DetailInfoFragment newInstance(SomeEntity someEntity) 
{
    DetailInfoFragment fragment = new DetailInfoFragment();
    Bundle args = new Bundle();
    args.putParcelable(ARG_PARAM1, someEntity);
    fragment.setArguments(args);
    return fragment;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM