Reputation: 390
I am trying to highlight tab icon when viewPager is selected or swiped. For this I am using 'tabLayout.setOnTabSelectedListener()'
. But it doesn't highlight when I swipe tab, but when I press tab selected all work perfect and tab gets highlighted. May be it's related with gradle version. I am using 'classpath 'com.android.tools.build:gradle:2.1.0''
. This is activity :
private int[] tabIcons = {
R.drawable.tab_icon_home,
R.drawable.tab_icon_cart2,
R.drawable.tab_icon_aksia,
R.drawable.tab_icon_menu
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon_tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(pageChangeListener);
viewPager.setOffscreenPageLimit(10);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
int currentPosition = 0;
@Override
public void onPageSelected(int newPosition) {
FragmentLifecycle fragmentToHide = (FragmentLifecycle) adapter.getItem(currentPosition);
fragmentToHide.onPauseFragment();
FragmentLifecycle fragmentToShow = (FragmentLifecycle) adapter.getItem(newPosition);
fragmentToShow.onResumeFragment();
currentPosition = newPosition;
adapter.notifyDataSetChanged();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) { }
};
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.setOnTabSelectedListener( new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
super.onTabUnselected(tab);
tab.getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.MULTIPLY);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
super.onTabReselected(tab);
}
});
}
Upvotes: 2
Views: 2366
Reputation: 390
I found solution,in gradle I used
dependencies {
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0' }
And I change from 23.0.0 to 23.0.1 in both appcompat-v7 and design. Now all work for me.
Upvotes: 1
Reputation: 2670
When you call setupWithViewPager
, this will internally call setOnTabSelectedListener(new ViewPagerOnTabSelectedListener(viewPager));
, overriding your OnTabSelectedListener
.
you have implemented TabLayout.ViewPagerOnTabSelectedListener
, and then overridden onTabSelected()
and call setOnTabSelectedListener()
after setupWithViewPager()
:
This is correct and it is working fine when tab clicked,
You are saying that when you scroll the page, it is not working. Have a look at your onPageSelected()
of OnPageChangeListener()
. Because this method gets called as and when particular Page selected.
Upvotes: 0
Reputation: 5534
Try this way...
tabLayout = ((TabLayout) findViewById(R.id.tabs));
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
public void onTabReselected(TabLayout.Tab paramAnonymousTab) {
}
public void onTabSelected(TabLayout.Tab paramAnonymousTab) {
viewPager.setCurrentItem(paramAnonymousTab.getPosition());
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
}
public void onTabUnselected(TabLayout.Tab paramAnonymousTab) {
tab.getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.MULTIPLY);
}
});
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
viewPager.setOffscreenPageLimit(4);
Happy coding.
Upvotes: 0