Last active
November 19, 2019 14:35
-
-
Save avianey/3a5eb0bbe3d385f4da2d6730a8a2564e to your computer and use it in GitHub Desktop.
Add Tab with custom click handler along with ViewPager Android Material component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TabLayout tabLayout = findViewById(R.id.tabs); | |
if (tabLayout != null) { | |
tabLayout.setupWithViewPager( | |
pager, | |
false // avoid listen for change of items in the adapter | |
); | |
try { | |
// Add custom tabs with managed click handler | |
Method m = tabLayout.getClass().getDeclaredMethod("createTabView", TabLayout.Tab.class); | |
m.setAccessible(true); | |
TabLayout.Tab tab = new TabLayout.Tab() { | |
@Override | |
public void select() { | |
// handle click yourself | |
} | |
}; | |
tab.parent = tabLayout; | |
tab.view = (TabLayout.TabView) m.invoke(tabLayout, tab); | |
tab.view.setMinimumWidth(0); | |
tabLayout.addTab(tab.setIcon(R.drawable.down), false); | |
} catch (Exception ignore) { | |
ignore.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment