Original Answer:
The Android team has changed a few things about how TabLayout and ViewPager talk to each other. Read the docs. But things are not explained well. I've included a lot of comments in the code. I hope that helps.
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
Adapter adapter = new Adapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
// the tabs will get their titles from the adapter and get populated
tabLayout.setTabsFromPagerAdapter(adapter);
// this is done "so that the tab position is kept in sync"
// what it does is when you swipe the fragment in view pager, it updates the tabs
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
// without this listener the tabs would still get updated when fragments are swiped, but .... (read the next comment)
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Toast.makeText(MainActivity.this, "tabSelected: " + tab.getText(), Toast.LENGTH_SHORT).show();
// no where in the code it is defined what will happen when tab is tapped/selected by the user
// this is why the following line is necessary
// we need to manually set the correct fragment when a tab is selected/tapped
// and this is the problem in your code
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Toast.makeText(MainActivity.this, "tabReSelected: " + tab.getText(), Toast.LENGTH_SHORT).show();
// Reload your recyclerView here
}
});
Check out this issue if you have got any other problems.
EDIT 1: December 2015
Not a solution to this question but helpful in general.
tabLayout.setupWithViewPager(viewPager);
This way you don't need to worry about setting the fragment yourself when a tab is selected. tabLayout.setOnTabSelectedListener(..)
is no longer needed in this situation. That is handled under the hood. This is useful when you don't need too much control over your tabs(like reloading fragment when same tab is selected/tapped).
UPDATE: MAY 2018
tabLayout.setTabsFromPagerAdapter(adapter);
tabLayout.setOnTabSelectedListener(...);
Both of the above functions are deprecated. Initialize viewpager+tablayout as shown below:
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager); // this will automatically bind tab clicks to viewpager fragments
viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))
// do additional tab clicks here
// no need to manually set viewpager item based on tab click
tabLayout.addOnTabSelectedListener(...);
OnPageChangeListener
is triggered when "page is changed". Are you usingTabLayout
for the tabs? – Carbuncle