-1

tablayout with scroll

I want to scroll the selector(pink line) to the next tab(on-fillup). my fragments are scrolling but the selector is unable to scroll. can anyone help?

MainActivity.java

//Adding the tabs using addTab() method
    tabLayout.addTab(tabLayout.newTab().setText("On Reserved"));
    tabLayout.addTab(tabLayout.newTab().setText("On Fill-up"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

activity_main.java

<!-- our tablayout to display tabs  -->
<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"></android.support.v4.view.ViewPager>

4 Answers4

5
PagerAdapter pagerAdapter = new PagerAdapter(getChildFragmentManager());
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(mBinder.viewPager);//This line is important to make tab scrollable with viewpager and it should be after set adapter to viewpager
Solulab Inc.
  • 107
  • 9
1

Try this solution

TabTextColor sets the color for the title of the tabs, passing a ColorStateList here makes tab change colors in different situations such as selected, active, inactive etc. TabIndicatorColor sets the color for the indiactor below the tabs.

tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.drawable.tab_selector));
tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.indicator));

Hope this will work for you.

Yash
  • 64
  • 4
Rashpal Singh
  • 633
  • 3
  • 13
1

you have to assign tablayout in viewpager like below code

// listener for tablayout when you select tab for change view
tabLayout.setOnTabSelectedListener(this);
 // assign tablayout in viewpager
 viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

this is override method when you tap on tab then viewpager is change

@Override
public void onTabSelected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
Ravi Patel
  • 309
  • 2
  • 13
0

This problem occur when you dont set viewpager with tablayout and viceversa too. So, please add following lines to your code...

PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), nooftabs);
viewpager.setAdapter(pagerAdapter);

tabLayout.setupWithViewPager(viewpager, true);

Hope this may help you

Biscuit
  • 4,840
  • 4
  • 26
  • 54
Deeksha
  • 536
  • 5
  • 14