Я использовал нижнюю часть меню администрирования в моем приложении
И я использовал TabLayout в одном из меню
Проблема: при двойном щелчке по tabLayout отображается только заголовок TabLayout, который не отображает содержимое ViewPager
Код моего приложения
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/1.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
setContentView(R.layout.activity_home_main);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Menu menu = navigation.getMenu();
selectFragment(menu.getItem(0));
disableShiftMode(navigation);
}
private void selectFragment(MenuItem item) {
item.setChecked(true);
int id = item.getItemId();
switch (id) {
case R.id.menu_home:
setFragment(new homeFragment());
break;
case R.id.menu_tri:
setFragment(new triFragment());
break;
case R.id.menu_four:
setFragment(new nemayeFragment());
break;
case R.id.menu_for:
setFragment(new forFragment());
break;
}
}
private void setFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content, fragment);
fragmentTransaction.commit();
}
Для tabLayout
public forFragment() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.forfragment, container, false);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
setupViewPager(viewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
Util.ViewPagerAdapter adapter=new Util.ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new nemayeFragment(), "ONE");
adapter.addFragment(new homeFragment(), "TWO");
adapter.addFragment(new nemayeFragment(), "THREE");
adapter.addFragment(new homeFragment(), "gfgd");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
Toast.makeText(getContext(), position+"", Toast.LENGTH_SHORT).show();
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}