Skip to content

Instantly share code, notes, and snippets.

@avianey
Last active November 19, 2019 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avianey/3a5eb0bbe3d385f4da2d6730a8a2564e to your computer and use it in GitHub Desktop.
Save avianey/3a5eb0bbe3d385f4da2d6730a8a2564e to your computer and use it in GitHub Desktop.
Add Tab with custom click handler along with ViewPager Android Material component
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