androidnavigationbaroncreateandroid-bottomnav

How do I switch the Activity with the bottomnavigation bar


I created a new project with the bottom-bar activity. This is the generated code:

package com.aaron.waller.mrpolitik;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

import com.aaron.waller.mrpolitik.tabs.KommentareFragment;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    Intent fragen = new Intent(MainActivity.this, KommentareFragment.class);
                    startActivity(fragen);
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
            }
            return true;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

}

And I want to set these three fragments on the the navigation bar: FirstFragment SecondFragment ThirdFragment

Also I want to swipe between the Fragments, how can I do that?


Solution

  • You can not load Fragments through Intent. To load fragments by BottomNavigationBar you can do as following:

    First add FrameLayout to our activity_main.xml:

      <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    Then replace this container with Fragments onNavigationItemSelected:

     @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                FragmentManager fm = getFragmentManager();
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        mTextMessage.setText(R.string.title_home);
                        fm.beginTransaction().replace(R.id.container, new FirstFragment()).commit();                      
                    case R.id.navigation_dashboard:
                        mTextMessage.setText(R.string.title_dashboard);
                        fm.beginTransaction().replace(R.id.container, new SecondFragment()).commit();  
                    case R.id.navigation_notifications:
                        mTextMessage.setText(R.string.title_notifications);
                        fm.beginTransaction().replace(R.id.container, new ThirdFragment()).commit();  
                }
                return true;
            }
    

    To, create swipe effect between fragments you can use ViewPager. Here is a nice tutorial how to use ViewPager to swipe between fragments. Hope this helps.