底部导航的一个选项卡中有多个片段

时间:2018-12-22 19:08:40

标签: android android-fragments bottomnavigationview

在我的应用中,我有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();
        }

1 个答案:

答案 0 :(得分:0)

可能您想在Instagram中做类似的事情。您所有的容器都应具有自己的堆栈历史记录。

首先,创建主机活动,其中将包含底部导航菜单和容器。

接下来,创建片段容器,如果您有5个菜单项,则在其中创建5个容器,则可以创建自己的导航逻辑。然后,如果用户单击菜单项,则应分离活动容器,并附加选定的容器。下面是带有详细信息的屏幕截图。

enter image description here