Why it is not possible to use ViewPager within a Fragment? It actually is

后端 未结 4 1369
庸人自扰
庸人自扰 2020-12-02 08:34

There are information that it is impossible to use ViewPager within a Fragment in many sources like "The Busy Coders Guide for Android Develop

相关标签:
4条回答
  • 2020-12-02 09:17

    While Initializing the SetionPageAdapter for ViewPager use "getChildFragmentManager()".

    mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());

    0 讨论(0)
  • 2020-12-02 09:18

    UPDATE: Since this answer was originally written, you now can have nested fragments, which means it is possible to have a ViewPager use fragments for pages and be in a fragment itself. This sample project demonstrates the technique.

    I now return you to your regularly-scheduled answer, presented in its entirety...


    Quoting myself from the book:

    The simplest way to use a ViewPager is to have it page fragments in and out of the screen based on user swipes. This only works if the ViewPager itself is not contained within a fragment, as you cannot have fragments nested inside of other fragments.

    Quoting Dianne Hackborn:

    Nested fragments are not currently supported. Trying to put a fragment within the UI of another fragment will result in undefined and likely broken behavior.

    It is perfectly possible to put a ViewPager inside a Fragment, so long as the contents of the ViewPager do not themselves contain fragments. Since the concrete implementations of PagerAdapter supplied by the Android Support package use fragments, you have to roll your own fragment-less PagerAdapter to put the ViewPager in a fragment.

    I will endeavor to make this point clearer in the next edition of the book (unless you're British, in which case I'll endeavour to make this point clearer :-).

    0 讨论(0)
  • 2020-12-02 09:19

    it is possible try to do this code and save view_pager_fragment.xml

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    

    ViewPagerFragment

    ViewPager viewPager;
    TabLayout tabLayout;
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tracks, container, false);
    
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        tabLayout = (TabLayout) view.findViewById(R.id.tab);
    
        return view;
    }
    
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        setupViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);
    
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
    
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
    
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
            }
        });
    }
    
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());``
    
        viewPagerAdapter.addFragment(new FirstFragment(), "First");
        viewPagerAdapter.addFragment(new SecoundFragment(), "Secound");
        viewPagerAdapter.addFragment(new LoginFragment(), "Login");
    
        viewPager.setAdapter(viewPagerAdapter);
    }
    
    private class ViewPagerAdapter extends FragmentPagerAdapter {
    
        List<Fragment> fragmentList = new ArrayList<>();
        List<String> fragmentTitles = new ArrayList<>();
    
        public ViewPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }
    
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }
    
        @Override
        public int getCount() {
            return fragmentList.size();
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentTitles.get(position);
        }
    
        public void addFragment(Fragment fragment, String name) {
            fragmentList.add(fragment);
            fragmentTitles.add(name);
        }
    }
    
    0 讨论(0)
  • 2020-12-02 09:31

    To implement the View pager within a fragment use getChildFragmentManager() instead of getFragmentManager(). You can call setAdapter() for the ViewPager from onCreateView() or onActivityCreated(), that is not a matter.

    I hope this will help you guys.

    0 讨论(0)
提交回复
热议问题