简体   繁体   中英

How can i use bottom navigation view referring to fragments and activities?

I want to implement logic to my BaseActivity.java where I can switch between activities and fragments.

I came upon only for solutions with only fragments: https://www.truiton.com/2017/01/android-bottom-navigation-bar-example/ or only activities: How can I implement the bottom navigation bar into an app for android? How can I implement the bottom navigation bar into an app for android?

but I don't know how to combine them

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:fabAlignmentMode="center"
        app:contentInsetRight="0dp"
        app:contentInsetEnd="0dp"
        app:contentInsetStart="0dp"
        app:contentInsetLeft="0dp"
        android:layout_gravity="bottom|center" >

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:foregroundGravity="bottom"
            app:itemIconTint="@color/primary_icon_color"
            app:itemRippleColor="@color/primary_icon_color_super_light"
            app:itemTextColor="@color/primary_icon_color"
            app:labelVisibilityMode="labeled"
            app:menu="@menu/bottom_nav_menu" />

    </com.google.android.material.bottomappbar.BottomAppBar> 

First of all you need to create a menu folder in res and create a new file named navigation.xml, You can adjust according to your needs. Below is a sample code in navigation.xml that I use:

<item
    android:id="@+id/navigation_home"
    android:icon="@drawable/ic_nav_home"
    android:title="Dahboard" />

<item
    android:id="@+id/navigation_category"
    android:icon="@drawable/ic_wallet"
    android:title="E-Wallet" />

<item
    android:id="@+id/navigation_promo"
    android:icon="@drawable/ic_promo"
    android:title="Promo" />

<item
    android:id="@+id/navigation_wishlist"
    android:icon="@drawable/ic_no_favorite"
    android:title="Wishlist" />

because you have called bottom navigation in BaseActivity Layout then you just need to add some code in OnCreate Block code in your BaseActivity.java:

viewPager = findViewById(R.id.viewpager);
    viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    viewPager.setOffscreenPageLimit(pager_number);
    createNotificationChannel();
    getToken();
    navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(item -> {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                viewPager.setCurrentItem(0);
                return true;
            case R.id.navigation_category:
                viewPager.setCurrentItem(1);
                return true;
            case R.id.navigation_promo:
                viewPager.setCurrentItem(2);
                return true;
            case R.id.navigation_wishlist:
                viewPager.setCurrentItem(3);
                return true;
        }
        return false;
    });

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }
        @Override
        public void onPageSelected(int position) {
            if (prevMenuItem != null) {
                prevMenuItem.setChecked(false);
            } else {
                navigation.getMenu().getItem(0).setChecked(false);
            }
            navigation.getMenu().getItem(position).setChecked(true);
            prevMenuItem = navigation.getMenu().getItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

and then you can add this code bellow your onCreate Block code:

    public class MyAdapter extends FragmentPagerAdapter {

    MyAdapter(FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new HomeFragment();
            case 1:
                return new EwalletFragment();
            case 2:
                return new PromoFragment();
            case 3:
                return new WishlistFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return pager_number;
    }

}

Don't forget to initialize variable abovr onCreate Block code:

public BottomNavigationView navigation;
public ViewPager viewPager;
MenuItem prevMenuItem;
int pager_number = 4;

I hope the above solution can solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM