Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the starting tab in tab layout with Toolbar?

I'm sorry I am not fluent in English. I wanna change the starting fragment in tab layout. There is a value from another activity that gave with intent. so I just want to start tab layout's fragment with my selection.

This is main activity's onCreate()

public class MainActivity extends AppCompatActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

    }

And this is the code of changing in fragment

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
                super(fm);

        }

        @Override
        public Fragment getItem(int position) {
            switch(position)
            {
                case 0:
                    return new TotalFragment();
                case 1:
                    return new StatsFragment();
                case 2:
                    return new FeedbackFragment();
                case 3:
                    return new ManageFragment();
            }
            return null;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "TOTAL";
                case 1:
                    return "STATS";
                case 2:
                    return "FEEDBACK";
                case 3:
                    return "MANAGE";
            }
            return null;
        }
    }
}
like image 626
jisu lee Avatar asked Oct 27 '25 10:10

jisu lee


1 Answers

Jisu Lee, we can set the starting tab in tab layout with the help of setCurrentItem() to viewpager. We can set it by two types :

Example : 1.Simple setting tab:

Explation : Set the currently selected page. If the ViewPager has already been through its first layout with its current adapter there will be a smooth animated transition between the current item and the specified item.

 viewPager.setCurrentItem(1);

2.For enableing smoothScroll :

Secand parameter "True" to smoothly scroll to the new item, "false" to transition immediately

 viewPager.setCurrentItem(1, true);

Or for getting the CurrentItem we have : viewPager.getCurrentItem();

It will return the starting tab index (int).

like image 66
A-Droid Tech Avatar answered Oct 29 '25 00:10

A-Droid Tech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!