How do I use FragmentPagerAdapter to have tabs with different content?

前端 未结 4 1277
迷失自我
迷失自我 2020-12-04 10:29

I want to have different tabs, where you can swipe through like in the android market. Each tab should use one fragment and use one method for it.

This is my Fragmen

相关标签:
4条回答
  • 2020-12-04 11:01

    Create the adapter:

    class MyPagerAdapter extends FragmentPagerAdapter {
            public List<String> fragmentsA; 
    
            public MyPagerAdapter(FragmentManager fm) {
                super(fm);
                fragmentsA = fragments;
            }
    
            @Override
            public Fragment getItem(int position) {
                //return MyFragment.newInstance();
                return Fragment.instantiate(context, fragmentsA.get(position));
    
            }
    
            @Override
            public CharSequence getPageTitle(int position) {
                //return CONTENT[position % CONTENT.length].toUpperCase();
                return mEntries.get(position % CONTENT.length).toUpperCase();
            }
    
            @Override
            public int getCount() {
               // return CONTENT.length;
                return mEntries.size();
            }
    
            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
        }
    

    Each fragment belongs to different classes.

    So you have to define a fragment list (These go to the main FragmentActivity) :

    static MyPagerAdapter adapter;
    

    and fill it with the fragments:

    fragments.add(ConnectionFragment.class.getName());
    fragments.add(DataFragment.class.getName());
    fragments.add(Tab3.class.getName());
    //..etc
    

    Then you have to add the fragments to the adapter like this:

     pager = (ViewPager)findViewById(R.id.viewpager);
     adapter = new MyPagerAdapter(getSupportFragmentManager());
     pager.setAdapter(adapter);
    

    The MyPagerAdaper will go through all the fragments contained by the fragments list, then it will instantiate each of them.

    0 讨论(0)
  • 2020-12-04 11:07

    In your FragmentPagerAdapter#getItem method, do not create object of the Fragments. You need to return the Fragment in the following way -

    return Fragment.instantiate(context, ConnectionFragment.class.getName());
    

    This means that the method will look like this -

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
    
      public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);  
      }
    
      @Override
      public Fragment getItem(int position) {
        Fragment fragment =null;
        switch (position) {
            case 0:
                fragment = Fragment.instantiate(context, ConnectionFragment.class.getName());
                break;
            case 1:
                fragment = Fragment.instantiate(context, DataFragment.class.getName());
                break;              
            case 2:
                fragment = Fragment.instantiate(context, GraphFragment.class.getName());
                break;          }
        return fragment;    
      }
    
      @Override
      public int getCount() {
        // Show 3 total pages.
        return 3;
      }
    }
    
    0 讨论(0)
  • 2020-12-04 11:11

    See this example:

     public static class MyPagerAdapter extends FragmentPagerAdapter {
        private static int NUM_ITEMS = 3;
    
        public MyPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }
    
        // Returns total number of pages
        @Override
        public int getCount() {
            return NUM_ITEMS;
        }
    
        // Returns the fragment to display for that page
        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0: // Fragment # 0 - This will show FirstFragment
                return FirstFragment.newInstance(0, "Page # 1");
            case 1: // Fragment # 0 - This will show FirstFragment different title
                return FirstFragment.newInstance(1, "Page # 2");
            case 2: // Fragment # 1 - This will show SecondFragment
                return SecondFragment.newInstance(2, "Page # 3");
            default:
                return null;
            }
        }
    
        // Returns the page title for the top indicator
        @Override
        public CharSequence getPageTitle(int position) {
            return "Page " + position;
        }        
    }
    
    0 讨论(0)
  • 2020-12-04 11:11

    ViewPager is deprecated, use ViewPager2 instead.

    With the release of ViewPager2 1.0.0, the FragmentPagerAdapter and FragmentStatePagerAdapter classes for interacting with ViewPager have been deprecated.

    ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager.

    • Read this to implement slide between fragments using ViewPager2
    • Read this to create swipe views with tabs using ViewPager2
    • If your app already uses ViewPager, read this page to learn more about migrating to ViewPager2.
    0 讨论(0)
提交回复
热议问题