如何从底部导航视图打开活动

时间:2019-08-29 09:43:08

标签: android bottomnavigationview

我有一个底部导航视图。我想打开一个活动,而不是单击action.item3时的片段。 到目前为止,这是代码:

bottomNavigationView.setOnNavigationItemSelectedListener
                (new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.action_item1:
                                selectedFragment = HomeFragment.newInstance();
                                break;
                            case R.id.action_item2:
                                selectedFragment = SpotsSearch1Fragment.newInstance();
                                break;
                            case R.id.action_item3:
                                selectedFragment = ItemThreeFragment.newInstance();
                                break;
                            case R.id.action_item4:
                                selectedFragment = ItemFourFragment.newInstance();
                                break;
                            case R.id.action_item5:
                                selectedFragment = ItemFiveFragment.newInstance();
                                break;
                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });

我尝试更改案例R.id.action_item3上的操作:

Intent intent = new Intent(HomeActivity.this,ChatActivity.class);
                startActivity(intent);
                break;

但是很明显,我在行上遇到了异常:

transaction.replace(R.id.frame_layout, selectedFragment);

有没有一种方法可以使用底部导航来打开活动而不是片段?

2 个答案:

答案 0 :(得分:1)

  

尝试这种方式

您可以使用breakswitch()R.id.action_item3 return true;

示例代码

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;
                switch (item.getItemId()) {
                    case R.id.action_item1:
                    selectedFragment = HomeFragment.newInstance();
                    break;
                    case R.id.action_item2:
                    selectedFragment = SpotsSearch1Fragment.newInstance();
                    break;
                    case R.id.action_item3:
                    Intent intent = new Intent(HomeActivity.this,ChatActivity.class);
                    startActivity(intent);
                     return true;
                    case R.id.action_item4:
                    selectedFragment = ItemFourFragment.newInstance();
                    break;
                    case R.id.action_item5:
                    selectedFragment = ItemFiveFragment.newInstance();
                    break;
                }


                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout, selectedFragment);
                    transaction.commit();
                    return true;


            }
        });

答案 1 :(得分:1)

因为Fragment Transaction发生在switch case之外。使用函数分离Transaction逻辑并在需要时调用。

bottomNavigationView.setOnNavigationItemSelectedListener
        (new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.action_item1:
                selectedFragment = HomeFragment.newInstance();
                fragTransaction(selectedFragment);
                break;
            case R.id.action_item2:
                selectedFragment = SpotsSearch1Fragment.newInstance();
                fragTransaction(selectedFragment);
                break;
            case R.id.action_item3:
                Intent intent = new Intent(HomeActivity.this,ChatActivity.class);
                startActivity(intent);
                break;
            case R.id.action_item4:
                selectedFragment = ItemFourFragment.newInstance();
                fragTransaction(selectedFragment);
                break;
            case R.id.action_item5:
                selectedFragment = ItemFiveFragment.newInstance();
                fragTransaction(selectedFragment);
                break;
        }

        return true;
    }
});

private void fragTransaction(Fragment selectedFragment){
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout, selectedFragment);
    transaction.commit();

}