This is my setup. works fine for me.
//declare your tabs to be add on
TabLayout tlDailyView;
private TabLayout.Tab tabAppointment, tabSlots;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_daily_view, container, false);
initializeMembers();
setupTabLayout();
return view;
}
private void setupTabLayout() {
tlDailyView.addTab(tabAppointment, 0, true);
tlDailyView.addTab(tabSlots, 1, true);
tlDailyView.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
//open fragment at position 0 here
case 1:
//open fragment at position 1 here
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void initializeMembers() {
tabSlots = tlDailyView.newTab();
tabAppointment = tlDailyView.newTab();
tabAppointment.setText(R.string.tab_appts).select();
tabSlots.setText(R.string.tab_slots);
}
don't forget to initialize your tab layout above.