在我的应用中,我有5个片段。我使用底部导航来管理它们。但是在第一个片段中,我有recyclerview,当我单击回收者项目时,我需要在此选项卡(第一个选项卡)中打开第二个片段。
public class ActivityBottom extends AppCompatActivity {
final Fragment fragment1 = new FragmentMarker();
final Fragment fragment2 = new FragmentBookmark();
final Fragment fragment3 = new FragmentMap();
final Fragment fragment4 = new FragmentNotification();
final Fragment fragment5 = new FragmentAccount();
final FragmentManager fragmentManager = getSupportFragmentManager();
Fragment active = fragment1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomHelper.disableShiftMode(bottomNavigationView);
fragmentManager.beginTransaction().add(R.id.frame_container, fragment5, "5").hide(fragment5).commit();
fragmentManager.beginTransaction().add(R.id.frame_container, fragment4, "4").hide(fragment4).commit();
fragmentManager.beginTransaction().add(R.id.frame_container, fragment3, "3").hide(fragment3).commit();
fragmentManager.beginTransaction().add(R.id.frame_container, fragment2, "2").hide(fragment2).commit();
fragmentManager.beginTransaction().add(R.id.frame_container, fragment1, "1").commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
fragmentManager.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
return true;
case R.id.nav_bookmark:
fragmentManager.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
return true;
case R.id.nav_blog:
fragmentManager.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
return true;
case R.id.nav_notification:
fragmentManager.beginTransaction().hide(active).show(fragment4).commit();
active = fragment4;
return true;
case R.id.nav_account:
fragmentManager.beginTransaction().hide(active).show(fragment5).commit();
active = fragment5;
return true;
}
return false;
}
};
}
现在,当我尝试打开第二个片段时,他将在所有选项卡上方打开。
这是我打开第二个片段的方法
@Override
public void onItemClick(Marker marker) {
MarkerDetailsFragment markerDetailsFragment = new MarkerDetailsFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("marker", marker);
markerDetailsFragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container, markerDetailsFragment, "MarkerDetailsFragment")
.addToBackStack(null)
.commit();
}