Start fragment in BottomNavigationView

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

    alone with create this methor

    protected boolean openFragment(Fragment fragment){
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.content, fragment)
                .commit();
        return true;
    }
    

    to edit in the switch

    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home: return openFragment(OneFragment.newInstance("",""));
                case R.id.navigation_beehive: return openFragment(twoFragment.newInstance("",""));
                case R.id.navigation_notifications: return openFragment(otherFragment.newInstance("",""));
            }
            return false;
        }
    

    and for to finalize you can to initialize with a fragment in

     protected void onCreate(Bundle savedInstanceState) {
        ....
        openFragment(OneFragment.newInstance("",""));
    }
    
    0 讨论(0)
  • 2021-01-01 06:48

    I would just add, if you wanted to use your BottomNavigationView.OnNavigationItemSelectedListener()to select your start fragment you can use the setSelectedItemId() e.g. :

    Replace 'R.id.navigation_home' with your own start fragment.

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        BottomNavigationView navigation = (BottomNavigationView) 
        findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setSelectedItemId(R.id.navigation_home);
    }
    
    0 讨论(0)
  • 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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.exampl.MainActivity">
        <FrameLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/navigation" />
    </LinearLayout>
    

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

     <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="android"/>
        </LinearLayout>
    

    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.

    0 讨论(0)
  • 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

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Home Fragment"
            android:textStyle="bold"
            android:layout_gravity="center"/>
    </FrameLayout>
    

    **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

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dashboard Fragment"
            android:textStyle="bold"
            android:layout_gravity="center"/>
    </FrameLayout>
    

    **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

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Notification Fragment"
            android:textStyle="bold"
            android:layout_gravity="center"/>
    </FrameLayout>
    

    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;
                }
            });
    
    0 讨论(0)
提交回复
热议问题