简体   繁体   English

底部导航栏不切换片段

[英]Bottom Navigation bar doesn't switch Fragments

I (beginner) just coded a bottom navigation bar with help of this forum on Android Studio (API19) and it compiles start in the emulator, but I just can't figure out why it doesn't switch Fragments when you click the Items in the bar.我(初学者)只是在 Android Studio (API19) 上的这个论坛的帮助下编写了一个底部导航栏,它在模拟器中开始编译,但我无法弄清楚为什么当您单击其中的项目时它不切换 Fragments酒吧。 For every Case in the method in Java I created a fragment without any special code in it.对于 Java 方法中的每个 Case,我都创建了一个片段,其中没有任何特殊代码。 Despite that I implemented the design in the gradle in the beginning and added a floating button, but thats pretty much all I did.尽管我一开始在 gradle 中实现了设计并添加了一个浮动按钮,但这几乎就是我所做的。

Thank you so much for any help!非常感谢您的帮助!

This is my Java Code这是我的 Java 代码

public class MainActivity extends AppCompatActivity {

    //Bottom Navigation Bar
    Fragment currentFragment = null;
    FragmentTransaction ft;

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

        //Bottom Navigation Bar
        ft = getSupportFragmentManager().beginTransaction();
        currentFragment = new HomeFragment();
        ft.replace(R.id.main_frame_layout, currentFragment);
        ft.commit();

        //Listener Bottom Navigation Bar
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation_bar);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    // Method for Nav. Bar Listener
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {

                case R.id.nav_home:
                    currentFragment = new HomeFragment();
                    ft = getSupportFragmentManager().beginTransaction();
                    ft.replace(R.id.main_frame_layout, currentFragment);
                    ft.commit();
                    return true;

                case R.id.nav_overview:
                    currentFragment = new OverviewFragment();
                    ft = getSupportFragmentManager().beginTransaction();
                    ft.replace(R.id.main_frame_layout, currentFragment);
                    ft.commit();
                    return true;

                case R.id.nav_charts:
                    currentFragment = new ChartsFragment();
                    ft = getSupportFragmentManager().beginTransaction();
                    ft.replace(R.id.main_frame_layout, currentFragment);
                    ft.commit();
                    return true;

                case R.id.nav_targets:
                    currentFragment = new TargetsFragment();
                    ft = getSupportFragmentManager().beginTransaction();
                    ft.replace(R.id.main_frame_layout, currentFragment);
                    ft.commit();
                    return true;
            }
            return true;
        }
    };
}

And this is my XML code这是我的 XML 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/main_frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="72dp"
        android:src="@drawable/ic_add_white"
        android:layout_gravity="bottom|end"
        android:focusable="true"
        app:backgroundTint="@color/colorAccent"
        app:fabSize="normal"/>

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView

        android:id="@+id/navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@color/colorAccent"
        app:itemTextColor="@color/colorAccent"
        app:menu="@menu/navigation_items"/>

</RelativeLayout>

And here is the menu file where I generated the navigation items but I think its unimportant这是我生成导航项的菜单文件,但我认为它不重要

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_home_white_24dp"
        android:title="@string/nav_home"
        android:enabled="true"/>
    <item
        android:id="@+id/nav_overview"
        android:icon="@drawable/ic_format_list_bulleted_white_24dp"
        android:title="@string/nav_overview"
        android:enabled="true"/>
    <item
        android:id="@+id/nav_charts"
        android:icon="@drawable/ic_insert_chart_white_24dp"
        android:title="@string/nav_charts"
        android:enabled="true"/>
    <item
        android:id="@+id/nav_targets"
        android:icon="@drawable/ic_monetization_on_white_24dp"
        android:title="@string/nav_targets"
        android:enabled="true"/>

</menu>

Do that like below, It will work像下面那样做,它会起作用


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

   bottomNavigationView.setOnItemSelectedListener (new 
     NavigationBarView.OnItemSelectedListener () {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;
                switch (item.getItemId ()) {
                    case R.id.homeFragment:
                        selectedFragment = new HomeFragment ();
                        break;
                    case R.id.filterFragment:
                        selectedFragment = new OverViewFragment ();
                        break;
                    case R.id.profileFragment:
                        selectedFragment = new ChartsFragment();
                        break;
                    case R.id.basketFragment:
                        selectedFragment = new TargetFragment ();
                        break;
                }
                getSupportFragmentManager ().beginTransaction ().replace (R.id.your_fragment_container
                        , selectedFragment).commit ();
                return true;
            }
        });
    }

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

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