i am using viewpager and tab layout
viewPager.setAdapter(Adapter);
tablayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
int cPage;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
cPage = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
now when i swipe selected tab color is changing but when i call
viewPager.setCurrentItem(1, true);
its not changing new tab layout color . say i have 2 tabs when i am 1st it setting text color green for 1st tab and when i swipe it giving 2nd tab color green .. but when i call
viewPager.setCurrentItem(1, true);
in this my 1st tab is green not 2nd tab
Faced unique problem. When we set setCurrentItem. It does not change tablayout's tab. Then you have to addOnPageChangeListener on viewpager in which you have to select the tablayout's tab manually for selected viewpager's position. Then setupWithViewPager.
Note : setupWithViewPager needs to be set only after addOnPageChangeListener added. God knows why. This is what worked. if I setupWithViewPager before, it does not work. Again, almighty only knows.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
viewPager.setCurrentItem(position,false);
tabLayout.getTabAt(position).select();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
/*
NOTE: This is setup after addOnPageChangeListener. Don't know why but this is what works. Otherwise tabLayout.does not select.
*/
tabLayout.setupWithViewPager(this.viewPager);
In your activity:
// init TabLayout
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
if (mCallFrom.equalsIgnoreCase(ActivityConstantUtils.sCalledFromActPanel)) {
// To add Memory
tabLayout.addTab(tabLayout.newTab().setText(R.string.add_memory_caption));
tabLayout.addTab(tabLayout.newTab().setText(R.string.add_memory_effects));
// tabLayout.addTab(tabLayout.newTab().setText(R.string.add_memory_frames));
} else {
// To add Memory from Recipe & Blog
tabLayout.addTab(tabLayout.newTab().setText(R.string.add_memory_caption));
tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);
}
changeTabsFont(tabLayout);
showDivider(tabLayout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(this.getFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == 1) {
Utils.hideSoftKeyboard(view, getActivity());
}
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
//Style apply to tablayout
<style name="ActPanelTabLayout.Theme" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@color/memory_color_80</item>\
<item name="android:textColor">@color/act_tab_black</item>
<item name="tabSelectedTextColor">@color/act_tab_selector_text_color</item>
<item name="tabTextAppearance">@style/TextAppearance.Jacksonville.ActTab</item>
</style>
Try this worked for me...
tabLayout.setScrollPosition(position,0f,true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With