getChildFragmentManager () cannot be resolved or cannot be referenced

前端 未结 5 2073
夕颜
夕颜 2020-12-06 11:41

I know, there are many forum posts on this topic already, but none did solve my issue

My code looks like this:

private SectionsPager         


        
相关标签:
5条回答
  • 2020-12-06 12:27

    getChildFragmentManager() is a method of a Fragment. Since you are subclassing AppCompatActivity, you can't access it. If you are using the Fragment from the support library you can use getSupportFragmentManager()

    0 讨论(0)
  • 2020-12-06 12:27

    This is probably not the most elegant solution, but in my case I needed the viewpager to be running in its own fragment, while having the toolbar and tablayout reside in the MainAcitivy. Only drawback is that you have to hide the TabLayout manually in other Fragments which not use the Tab/Viewpager.

    But this way you can still call getChildFragmentManager from the Fragment.

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_rides_host, container, false);
    
        viewPager = (ViewPager) v.findViewById(R.id.fm_feed_host_viewpager);
    
        setupViewPager(viewPager);
    
        ((MainActivity)getActivity()).mTabLayout.setupWithViewPager(viewPager);
    
        return v;
    }
    
    private void setupViewPager(ViewPager viewPager) {
    
        ViewPagerAdapter adapter = new ViewPagerAdapter(this.getChildFragmentManager());
    
        adapter.addFragment(new YourFragment(), "Title");
        adapter.addFragment(...);
        viewPager.setAdapter(adapter);
    }
    
    0 讨论(0)
  • 2020-12-06 12:31

    I had exactly same issue and I managed to resolve it by simply adding:

    viewpager.setOffscreenPageLimit(2);
    

    As was suggested in this post.

    0 讨论(0)
  • 2020-12-06 12:34

    Using Kotlin, used with my Frgament I used:

    //using this.childFragmentManager
    //not as this.childFragmentManager()
    val sectionsPagerAdapter = CameraTabLayoutAdapter(this.childFragmentManager)
    
    0 讨论(0)
  • 2020-12-06 12:38

    you should do like:

    adapter = new ViewPagerAdapter(this.getChildFragmentManager());
    
    0 讨论(0)
提交回复
热议问题