How to use viewPagerAdapter

0

I have an activity called "visas" within this activity I have 5 other calls (seen 1 to 5), I want these visas to be next to each other, being able to slide horizontally between them through the viewpager adapter, however I did not find any tutorial to help me with this. Can someone help me ???

    
asked by anonymous 19.07.2017 / 01:55

1 answer

0

First you should create a layout with ViewPager:

<android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

Oncreate your Activity:

setContentView(R.layout.activity_main);
        ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);

Then you should create a class that will be your adapter:

public static class AppSectionsPagerAdapter extends FragmentPagerAdapter
        {
            public AppSectionsPagerAdapter(FragmentManager fm)
            {
                super(fm);
            }

            @Override
            public Fragment getItem(int i)
            {

                Fragment fragment = null;
                switch (i)
                {
                    case 0:
                        fragment = new  Main_fragment();

                        break;
                    case 1:
                        fragment = new Lock_Fragment();
                }
                return fragment;
            }

            @Override
            public int getCount()
            {
                return 2;
            }

            @Override
            public CharSequence getPageTitle(int position)
            {

                String tab = null;
                switch (position)
                {
                    case 0:
                        tab = "Main";

                        break;
                    case 1:
                        tab = "Bloqueio";

                }
                return tab;
            }
        }

Now you should put your adapter to ViewPager:

AppSectionsPagerAdapter mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

mViewPager.setAdapter(mAppSectionsPagerAdapter);

Remembering that "Main_fragment ();" and "Lock_Fragment ();" are fragmentation, you can create and register as many as you want, without forgetting to modify the:

@Override
            public int getCount()
            {
                return 2;

(In my case I returned two snippets)

And the titles for each position.

    
19.07.2017 / 19:26