得到错误的片段onnlick of bottomnavigation

时间:2018-04-24 04:27:47

标签: java android android-fragments bottomnavigationview

嗨我正在制作一个应用程序,我正在使用带有底部导航的片段但是当我点击主页按钮时它不加载主片段而是加载第二个片段而且当我启动应用程序主片段时默认不加载我知道这可能听起来很幼稚,但如果有人可以指导我,我会遇到这个问题 我的片段导航代码

public class MainActivity extends AppCompatActivity {
   FrameLayout frameLayout;
   FragmentManager fragmentManager;
   Fragment fragment;
   BottomNavigationView bottomNavigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //BottomNavigationView b=findViewById(R.id.bottom_nav);


        fragmentManager = getSupportFragmentManager();

       if(findViewById(R.id.frame_container)!=null){
            if(savedInstanceState!=null){

                return;
            }
            /*HomeFragment homeFragment = new HomeFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.frame_container, homeFragment,null);
            transaction.addToBackStack(null);
      /* Comment this line and it should work!*/
            //transaction.addToBackStack(null);
            //transaction.commit();
        }
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);

        //final SeekBar sb = (SeekBar) findViewById(R.id.sb);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment fragment = null;
                switch (item.getItemId()){
                    case R.id.navigation_home:
                        fragment = new HomeFragment();
                        loadFragment(fragment);

                    case R.id.navigation_feed:
                        fragment = new FeedFragment();
                        loadFragment(fragment);
                        return true;
                    case R.id.navigation_event:
                        fragment = new EventsFragment();
                        loadFragment(fragment);
                        return true;
                    case R.id.navigation_nearby:
                        fragment = new NearbyFragment();
                        loadFragment(fragment);
                        return true;
                    case R.id.navigation_profile:
                        fragment = new ProfileFragment();
                        loadFragment(fragment);
                        return true;
                }
                return false;

            }
        });

    }

    private void loadFragment(Fragment fragment) {
       // HomeFragment homeFragment = new HomeFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container, fragment,null);
        transaction.addToBackStack(null);
      /* Comment this line and it should work!*/
        //transaction.addToBackStack(null);
        transaction.commit();
        // load fragment
        //HomeFragment homeFragment = new HomeFragment();

    }

}

1 个答案:

答案 0 :(得分:1)

您忘记在 return true; case R.id.navigation_home: 查看

试试这个

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment fragment = null;
                switch (item.getItemId()){
                    case R.id.navigation_home:
                        fragment = new HomeFragment();
                        loadFragment(fragment);
                        return true;
                    case R.id.navigation_feed:
                        fragment = new FeedFragment();
                        loadFragment(fragment);
                        return true;
                    case R.id.navigation_event:
                        fragment = new EventsFragment();
                        loadFragment(fragment);
                        return true;
                    case R.id.navigation_nearby:
                        fragment = new NearbyFragment();
                        loadFragment(fragment);
                        return true;
                    case R.id.navigation_profile:
                        fragment = new ProfileFragment();
                        loadFragment(fragment);
                        return true;
                }
                return false;

            }
        });