How to properly use fragments with ViewPager?

后端 未结 1 552
一生所求
一生所求 2020-12-10 23:37

I know many applications using fragments in ViewPager. I need it in my application too. I have no found any guide how I can to do it exclude this. And here is my code:

1条回答
  •  时光说笑
    2020-12-11 00:16

    Even I had trouble understanding the concept at first and I won't say that I fully understood it. But,

    Here's how I am using fragment in my sample application:

    Step 1: Creating my Adapter:

    public class AllPagesAdapter extends FragmentStatePagerAdapter {
    
    
    
                public AllPagesAdapter(FragmentManager fm) {
    
                    super(fm);
    
                }
    
                @Override
                public Fragment getItem(int index) {
    
            switch (index) {
                case 0:
    
                    return new Fragment1();
    
                case 1:
    
                    return new Fragment2();
                case 2:
    
                    return new Fragment3();
    
                case 3:
    
                    return new Fragment4();
    
                case 4:
    
                    return new Fragment5();
    
    
            }
            return null;
    
    
        }
    
        @Override
        public int getCount() {
            return 5;
        }
    
    }
    

    Step 2: In the main activity, I am extending fragment activity and implementing an ActionBar.TabListener. Here's how I am doing it:

    public class SomeActivity extends FragmentActivity implements ActionBar.TabListener {
    
        public ViewPager viewPager;
        private AllPagesAdapter mAdapter;
        private ActionBar actionBar;
        private String [] tabs = {"Fragment1","Fragment2","Fragment3","Fragment4","Fragment5"};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Initializing all stuff
            viewPager = (ViewPager)findViewById(R.id.pager);
    
            actionBar = getActionBar();
            mAdapter = new AllPagesAdapter(getSupportFragmentManager());
            viewPager.setAdapter(mAdapter);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
    
    
            //Add the tabs here
            for(String tab_name:tabs){
                actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
            }
    
            viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
    
                @Override
            public void onPageSelected(int position){
    
                    //on Page change, that particular page should be selected
                    actionBar.setSelectedNavigationItem(position);
                }
    
                @Override
                    public void onPageScrolled(int arg0,float arg1,int arg2){
    
                }
                @Override
            public void onPageScrollStateChanged(int position){
    
                }
    
            });
        }
    
    
    
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
    
            viewPager.setCurrentItem(tab.getPosition());
        }
    
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
    
    
    
        }
    
        @Override
        public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
    
            viewPager.setCurrentItem(tab.getPosition());
    
        }
    }
    

    Step 3: I have just one fragment for this example:

    Here's the code:

    public class Fragment1 extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            //Can be layout you want it to be.
            View databaseview = inflater.inflate(R.layout.database, container, false);
    
            return databaseview;
    
        }
    
    }
    

    Step 4: Last part is the layout: This will be starting point.

    
    

    Lastly, one important thing is that, number of fragments should be equal to the string array defined in the main activity. If not, the app crashes.

    Hope I explained it well..:)

    I know this is a very old post but this answer can be used for future viewers.

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