我有一个选项卡式活动,单击底部导航视图按钮时需要显示,我需要将其转换为片段式
公共类MainActivityTabs扩展了AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
Fragment homeFragment;
Fragment searchFragment;
Fragment cameraFragment;
Fragment favoritesFragment;
Fragment profileFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maintabs);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
homeFragment = new HomeFragment();
searchFragment = new SearchFragment();
cameraFragment = new CameraFragment();
favoritesFragment = new FavoritesFragment();
profileFragment = new ProfileFragment();
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return homeFragment;
} else if (position == 1) {
return searchFragment;
} else if (position == 2) {
return cameraFragment;
} else if (position == 3) {
return favoritesFragment;
} else if (position == 4) {
return profileFragment;
}
return null;
}
@Override
public int getCount() {
return 5;
}
}
}
我需要使活动仅在单击底部导航视图中的按钮时出现。