简体   繁体   English

Android 活动中的底部导航栏

[英]Android Bottom Navigation Bar in activities

I am developing a car rental app and right now I have 3 fragments in my app (Home - where all the cars are displayed from a recyclerview, Orders - where user's bookings are shown, Profile - user profile).我正在开发一个汽车租赁应用程序,现在我的应用程序中有 3 个片段(主页 - 从回收站视图中显示所有汽车,订单 - 显示用户的预订,个人资料 - 用户个人资料)。 I can switch beetween these 3 fragments from my bottom navigationbar.我可以从底部导航栏中切换这 3 个片段。

Now I created onclick listeners for every car on home page.现在我为主页上的每辆车创建了 onclick 监听器。 When I click on a car it sends me to a new activity "activity_car_detail.xml" which contains specific information about the selected car.当我点击一辆车时,它会将我发送到一个新活动“activity_car_detail.xml”,其中包含有关所选汽车的特定信息。 My question is: how can I make the navigation bar to appear on this Car Detail activity?我的问题是:如何让导航栏出现在这个 Car Detail 活动中? Should I use fragment instead of activity or what should I do?我应该使用片段而不是活动还是应该怎么做?

This is the code for the Main Activity which contains the bottom navigation bar:这是包含底部导航栏的 Main Activity 的代码:

public class MainActivity extends AppCompatActivity {

public static ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);

    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}

private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;

        switch (item.getItemId()) {
            case R.id.nav_home:
                selectedFragment = new HomeFragment();
                break;
            case R.id.nav_orders:
                selectedFragment = new OrdersFragment();
                break;
            case R.id.nav_profile:
                selectedFragment = new ProfileFragment();
                break;
        }


        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();

        return true;
    }
};
}

I suggest using the Navigation component instead of Fragment transactions.我建议使用 Navigation 组件而不是 Fragment 事务。 Also this will fix your issue and it will remove boilerplate code.这也将解决您的问题,并将删除样板代码。 Read more about it's documentation here . 在此处阅读有关其文档的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM