如何实现底部导航栏事务不分段,而是对活动类?

w6mmgewl  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(282)

如果单击底部导航项,如何转换到活动而不是片段?

setContentView(R.layout.activity_main);
        fm.beginTransaction().add(R.id.main_container,fragment3).hide(fragment3).commit();
        fm.beginTransaction().add(R.id.main_container,fragment2).hide(fragment2).commit();
        fm.beginTransaction().add(R.id.main_container,fragment1).commit();        //this is fragment

        //this is implementing fragment  
        final BottomNavigationView bottomNavigationView = findViewById(R.id.nav_bawah);
        bottomNavigationView.setSelectedItemId(R.id.navigation_dashboard);
        bottomNavigationView.setOnNavigationItemSelectedListener(new 
        BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected( MenuItem item) {

                switch (item.getItemId()) {

                    case R.id.navigation_dashboard:

                        fm.beginTransaction().hide(active).show(fragment1).commit();
                        active=fragment1;

                        break;
                    case R.id.navigation_challange:

                        fm.beginTransaction().hide(active).show(fragment2).commit();
                        active=fragment2;

                        break;

                    case R.id.navigation_chat:

                        fm.beginTransaction().hide(active).show(fragment3).commit();
                        active = fragment3;

                        break;

                }
                return true;

            }
        });
3okqufwl

3okqufwl1#

使用 Intent 开始新的活动。

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected( MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_related_to_activity:
                Intent intent = new Intent(requireActivity(), YourActivity.class);
                startActivity(intent);
            break;
            // other cases ...
        }
        return true;

    }
});

相关问题