1
votes

In my App (Material Design) I would like to show (say) 10 tabs. Of those tabs, the tab strip should hold e.g. 4. Now the tabstrip shows all tab-titles with 2 letters per tab. How can I create a sliding tabstrip? So I can scroll through the tab strip to the right tab? I know there is a custom made tabstrip, I would like to use as much as possible standard facilities.

1 - Setting up the tab pager:

private void setupTabPager() {
    viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            // UserFeedback.show( "Tab selected: " + tab.getText());
            viewPager.setCurrentItem(tab.getPosition());
        }
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
}

2 - Setting up the tab layout:

private void setupTablayout() {
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    if (tabLayout == null) {
        return;
    }
    // tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabGravity(TabLayout.MODE_SCROLLABLE);
    tabLayout.addTab(tabLayout.newTab().setText("Folders"));
    tabLayout.addTab(tabLayout.newTab().setText("Items"));

3 - The PageAdapter extending FragmentStatePagerAdapter:

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;
    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case MainActivity.FRAGMENT_GROUPS_AND_ITEMS:
                return new FragmentGroupsAndItems();
            case MainActivity.FRAGMENT_FOLDERS:
                return new FragmentFolders();
     .... }
2

2 Answers

3
votes

See this tutorial. It will work for u. http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

It's according to new implementation of Android Material Design

1
votes

Why don't you put your tabs inside HorizontalScrollView ?

It might helps you.