Managing FragmentPagerAdapter in FragmentManager

Asked 2 years ago, Updated 2 years ago, 70 views

I am using FragmentPagerAdapter in Fragment, but when I display the Fragment below, it is displayed the first time, but since the second time, is the FragmentPagerAdapter?It is not called successfully and neither FragmentA nor FragmentB is displayed.
The FragmentPagerAdapter is managed by Fragmentmanager, so it was written that Fragmentmanager needs to be updated, so I think that's the cause, but what is the cause?
Also, I would appreciate it if you could tell me specifically how to solve the problem.I look forward to your kind cooperation.

public class Top extensions Fragment {

    public static Top newInstance() {
        return new Top();
    }

    @ Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @ Override
    publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.top, container, false);

        // Configure MyPagerAdapter for ViewPager
        ViewPager=(ViewPager)view.findViewById(R.id.pager);
        pager.setAdapter(new MyPagerAdapter(getFragmentManager()));

        // Configure ViewPager for PagerSlidingTabStrip
        PagerSlidingTabStrip=(PagerSlidingTabStrip)view.findViewById(R.id.tabs);
        tabs.setViewPager(pager);

        getActivity().setTitle("Test";

        return view;
    }

    @ Override
    public void onCreateOptionsMenu(Menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu, menu);
    }

    private class MyPagerAdapter extensions FragmentPagerAdapter{

        private final String[]TITLES={"A", "B"};

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

        @ Override
        public Fragment getItem(int position){
            switch(position){
                case0:
                    return new FragmentA();
                case1:
                    return new FragmentB();
            }

            return null;
        }


        @ Override
        public intgetCount(){
            return TITLES.length;
        }

        @ Override
        publicCharSequence getPageTitle(int position){
            return TITLES [position];
        }
    }
}

android android-fragments

2022-09-30 20:36

1 Answers

Since it will be Fragment in Fragment, it is correct to pass getChildFragmentManager().

 // Configure MyPagerAdapter for ViewPager
ViewPager=(ViewPager)view.findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));


2022-09-30 20:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.