Start fragment in BottomNavigationView

后端 未结 4 519
无人共我
无人共我 2021-01-01 06:30

I\'m working with a simple app with Bottom Navigation View. I have 3 fragment with text, and i want to start their when i select a item in Botton Navigation, but i don\'t kn

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-01 06:52

    Firstly, in your activity_main.xml we don't require 3 different fragments as we can replace or add our any selected Fragment in 1 FrameLayout only. Secondly, when ever the user select any one from the Bottom NavigationView just get an instance of the related Fragment class and replace it with your activity_main's FrameLayout .

    Fragment selectedFragment = null;
             switch (item.getItemId()) {
                   case R.id.navigation_home:
                       selectedFragment = FunFragment.newInstance();
                       break;
    

    after getting the instance of the selected Fragment replace it with you activity_main's FrameLayout as to show on the screen.

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.content, selectedFragment);
                transaction.commit();
    

    EDIT TAG Step 1. your activity_main.xml should look like this

    
    
        
        
    
    

    Step 2. Your fragment_home.xml layout should be like this

     
        
    
            
        
    

    Make 3 different fragment layout for your three different options

    Step 3. your MainActivity.java class will be like this

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
                    = new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem item) {
                    Fragment selectedFragment = null;
                    switch (item.getItemId()) {
                        case R.id.navigation_home:
                            selectedFragment = FunFragment.newInstance();
                           break;
                        case R.id.navigation_dashboard:
                            selectedFragment = TheoryFragment.newInstance();
                           break;
                        case R.id.navigation_notifications:
                            selectedFragment = AndroidFragment.newInstance();
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.content, selectedFragment);
                    transaction.commit();
                    return true;
                }
            };
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
    
            navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.content, FunFragment.newInstance());
            transaction.commit();
        }
    

    In on create replace with the fragment which you want to show after launching the application and navigation listener will take care of what ever option you will select

    Step 4. Your will have 3 different Fragment class and look like this

    public class TheoryFragment extends Fragment {
    
         public static TheoryFragment newInstance() {
                TheoryFragment fragment = new TheoryFragment();
                return fragment;
            }
         @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_theory, container, false);
                unbinder = ButterKnife.bind(this, rootView);
                return rootView;
            }
        }
    

    Hope it will help you, let me know if any problem.

提交回复
热议问题