我的底部导航栏仅在我双击切换片段时才有效

时间:2019-07-12 11:24:27

标签: android android-fragments bottomnavigationview

我创建了一个底部导航栏,可以在4个片段之间切换。它工作正常,但是,我需要双击菜单项才能在片段之间进行切换。这不是我想要的功能,也不知道它是怎么实现的。

我所关注的所有教程都需要单击一下才能在片段之间切换,所以我很困惑。

我的MainActivity.java

public class MainActivity extends AppCompatActivity
     {
private FirebaseAuth auth;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        auth = FirebaseAuth.getInstance();
        if (auth.getCurrentUser() == null) {
            startActivity(new Intent(MainActivity.this, Login_Page_Activity.class));
        }
        BottomNavigationView bottomnav = findViewById(R.id.bottom_navigation);
        bottomnav.setOnNavigationItemReselectedListener(navListener); //this
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new explore_fragment()).commit();
    }

        private BottomNavigationView.OnNavigationItemReselectedListener navListener=
            new BottomNavigationView.OnNavigationItemReselectedListener() {
                @Override
                public void onNavigationItemReselected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = null;
                    switch (menuItem.getItemId()){
                        case R.id.nav_explore:
                            selectedFragment = new explore_fragment();
                            break;
                        case R.id.nav_add:
                            selectedFragment = new add_events_fragment();
                            break;
                        case R.id.nav_events:
                            selectedFragment = new events_fragment();
                            break;
                        case R.id.nav_chat:
                            selectedFragment = new Chats_fragment();
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                            selectedFragment).commit();

                }
            };

1 个答案:

答案 0 :(得分:0)

也许是问题ReselectedListener

尝试选择项目的听众

public class MainActivity extends AppCompatActivity
     {
private FirebaseAuth auth;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        auth = FirebaseAuth.getInstance();
        if (auth.getCurrentUser() == null) {
            startActivity(new Intent(MainActivity.this, Login_Page_Activity.class));
        }
        BottomNavigationView bottomnav = findViewById(R.id.bottom_navigation);
        bottomnav.setOnNavigationItemSelectedListener(navListener); //this
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new explore_fragment()).commit();
    }

        private BottomNavigationView.OnNavigationItemSelectedListener navListener=
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public void onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = null;
                    switch (menuItem.getItemId()){
                        case R.id.nav_explore:
                            selectedFragment = new explore_fragment();
                            break;
                        case R.id.nav_add:
                            selectedFragment = new add_events_fragment();
                            break;
                        case R.id.nav_events:
                            selectedFragment = new events_fragment();
                            break;
                        case R.id.nav_chat:
                            selectedFragment = new Chats_fragment();
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                            selectedFragment).commit();

                }
            };