I have a CoordinatorLayout
which contains AppBarLayout
and a FrameLayout
which contains fragments.
One of this fragment contai
To fix Bottom bar, firstly you have to use a Activity where tabs present. In that activity use different fragments. Add your tabLayout in this activity_main_tab.xml
activity_main_tab.xml
For fragment view use
fragment_tab.xml
here use app:layout_scrollFlags="scroll|enterAlways" in toolbar to make it scrollable
FragmentTabs.java
public class FragmentTabs extends BaseFragment{
private View rootView;
private ViewPager viewPager;
private TabLayout tabLayout;
private Toolbar toolbar;
private ViewPagerAdapter viewPagerAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroupContainer, Bundle savedInstanceState) {
rootViewSearch = inflater.inflate(R.layout.fragment_tab, viewGroupContainer, false);
initializeLayout();
setUpActionBar();
setUpFragmentObjects();
return rootView;
}
private void initializeLayout() {
toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout);
}
private void setUpActionBar() {
getApplicationContext().setSupportActionBar(toolbarSearch);
ActionBar actionBar = getApplicationContext().getSupportActionBar();
if(actionBar != null){
actionBar.setTitle(Constants.BLANK);
}
}
private void setUpFragmentObjects() {
viewPagerAdapter = new ViewPagerAdapter(getApplicationContext(), getChildFragmentManager());
viewPager.setAdapter(searchViewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(Activity activity, FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
Bundle bundle;
switch (position) {
case 0: // For Tab 1
// use fragment for tab 1
return fragment1;
case 1: // For Tab 2
// use fragment for tab 2
return fragment2;
case 2: // For Tab 3
// use fragment for tab 3
return fragment3;
default:
return null;
}
}
@Override
public int getCount() {
return 3; // no. of Tabs
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: // Title For Tab1
return "Tab1";
case 1: // Title For Tab2
return "Tab2";
case 2: // Title For Tab3
return "Tab3";
default:
return null;
}
}
}