Start fragment in BottomNavigationView

后端 未结 4 537
无人共我
无人共我 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:55

    Take three Fragments like HomeFragment,DashboardFragment and NotificationFragment and three layouts for those three fragments.

    HomeFragment

    public class HomeFragment extends Fragment {
        public HomeFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_home, container, false);
        }
    }
    

    fragment_home.xml

    
    
        
    
    

    **DashboardFragment **

    public class DashboardFragment extends Fragment {
        public DashboardFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_dashboard, container, false);
        }
    }
    

    fragment_dashboard.xml

    
    
        
    
    

    **NotificationFragment **

    public class NotificationFragment extends Fragment {
        public NotificationFragment () {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_notification, container, false);
        }
    }
    

    fragment_notification.xml

    
    
        
    
    

    This is Your activity.

    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();
                    switch (id){
                        case R.id.Fragment_one:
                            fragment = new HomeFragment();
                            break;
                        case R.id.Fragment_two:
                            fragment = new DashboardFragment();
                            break;
                        case R.id.Fragment_three:
                            fragment = new NotificationFragment();
                            break;
                    }
                    final FragmentTransaction transaction = fragmentManager.beginTransaction();
                    transaction.replace(R.id.main_container, fragment).commit();
                    return true;
                }
            });
    

提交回复
热议问题