I created an activity with a bottom navigation bar
.
I want tablayout
when I want to switch to another fragment.
And tablayout
can con
You need to put view pager on Activity. Make a view pager Adapter. Connect your different fragment in your Adapter. After that you can set your Adapter on your Activity example is given below. O sorry we need to put your tab layout before vewPager. Connect your tab layout on your View Pager Adapter.
public class MainActivity extends Activity {
ViewPager viewpager;
TabLayout tabLayout;
public void onCreate(Bundle savedInstanceState) {
viewpager = findViewById(R.id.viewpager);
tabLayout = findViewById(R.id.tablayout);
super.onCreate(saveedInstanceState);
.........
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewpager.setViewPager(adapter);
tabLayout.setupWithViewPager(viewPager);
}
}
public class ViewPagerAdapter extends FragmentPagerAdapter {
private String tabTitles [] = new String[] {"title first", "title Second"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new YourFragmentFirst();
} else if (....){
return new YourFragmentSeconde();
}
........
}
@Override
public int getCount() {
return //return your number of Fragment;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}