This article in my personal website, my Jane book, my CSDN blog published at the same time, please pay more attention to!

preface

Last week’s article taught you to simply play with the ViewPager (1) guide page with Pointers. It dug a hole about learning ViewPager, and this week it will definitely be filled in. This series of pits was written entirely in the same Demo, so hopefully from the first one, the code is fairly easy.

Ps: I don’t know if you have seen the article I wrote before, RadioButton+Fragment to achieve the bottom navigation bar, yes, this time I still use this layout to write Demo, so there will be more fragments, please distinguish them. (The effect is to add 3 slidable fragments to a Fragment, ha!) Look at the code!

ViewPager+TabLayout+Fragment top TAB




The interface slip


Talk is cheap,show u the code

The compile 'com. Android. Support: design: 23.4.0' compile 'com. Android. Support: support - v4:23.4.0'Copy the code
    

    

    

    Copy the code
  • Code on Fragment1 (ps:Fragment1 contains 3 VPFragment sliders)
private ViewPager mViewPager1; private TabLayout mTabLayout; Private String[] tabTitle = {" tabTitle "," tabTitle "," tabTitle "}; Nullable @override public View onCreateView(LayoutInflater inflater, @nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment1_layout, container, false); initViews(rootView); initData(); return rootView; } private void initViews(View rootView) { mViewPager1 = (ViewPager) rootView.findViewById(R.id.mViewPager1); mTabLayout = (TabLayout) rootView.findViewById(R.id.mTabLayout); } private void initData() { for (int i=0; iCopy the code

Because of combined fragments are used together, so that the adapter is required for the FragmentStatePagerAdapter, also is easy to use, as follows:

private String[] tabTilte; public MyFragmentStatePagerAdapter(FragmentManager fm, String[] tabTitle) { super(fm); this.tabTilte = tabTitle; } @Override public Fragment getItem(int position) { switch (position) { case 0: return new VPFragment1(); Fragment Case 1: return new VPFragment2(); // fragment Case 2: return new VPFragment3(); } return null; } @Override public int getCount() { return tabTilte.length; }Copy the code

To be continued

Please note that in the next post, a case study teaches you to simply play with the automatic rotation effect of ViewPager (3) with pointer

Little brother is not talented, but also hope more advice!