Reputation: 516
I try to use create a tab layout like google play. But I couldn't catch click event. My code is below:
tabLayout.setupWithViewPager(viewPager);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.i("tag", " onTabSelected: " + tab.getPosition());
}
.....
});
}
});
onTabSelected never called.
EDIT
Below code work only swiping.
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
...
}
But I want to item click event.
Upvotes: 2
Views: 565
Reputation: 516
Sometimes I believe the problem might be that the ViewPager is positioned above the TabLayout. You want to place it below like this:
click on tab layout doesn't work at all
Upvotes: 0
Reputation: 16976
You won't need a Thread
.
Instead, try this:
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.i("tag", " onTabSelected: " + tab.getPosition()); // return the position of the selected Tab
Toast.makeText(mainActivity.this, "Clicked", Toast.LENGTH_SHORT).show() // this should show the clicked when selecting a tab
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Upvotes: 1