一个增强BottomNavigationView的安卓库。

田兴旺
2023-12-01

官方文档

实现:

第一步:添加依赖

 implementation 'com.github.ittianyu:BottomNavigationViewEx:2.0.2'
   implementation  "com.android.support:design:28.0.0"

第二步:布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activity.HomeActivity">

    <com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        app:itemHorizontalTranslationEnabled="false"
        app:itemTextColor="@color/bottom_navigation"
        app:menu="@menu/menu_bottom_navigation"
        app:itemBackground="@null">

    </com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx>

    <FrameLayout
        android:id="@+id/content_page_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation">
    </FrameLayout>

</RelativeLayout>

第三步:Activity使用

package com.example.bottomnavigationviewdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.accounts.AccountManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.View;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationViewEx mBottomNavigationViewEx;
    private static final int DEFAULT_SELECTED_MENU_ITEM_POSITION = 0;
    private Fragment mCurrentShownFragment;
    private int mCurrentMenuItemPosition;
    private SparseArray<Fragment> mMenuItemPagerMapping = new SparseArray<>();

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

    private void initListeners() {
        mBottomNavigationViewEx.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switchFragment(menuItem.getItemId());
                return true;
            }
        });
    }

    private void initView() {
        mBottomNavigationViewEx = findViewById(R.id.bottom_navigation);
        mBottomNavigationViewEx.enableAnimation(false);
        mBottomNavigationViewEx.enableShiftingMode(false);
        mBottomNavigationViewEx.enableItemShiftingMode(false);
        mBottomNavigationViewEx.setItemIconTintList(null);
        mBottomNavigationViewEx.setTypeface(null, Typeface.BOLD);
        mBottomNavigationViewEx.setCurrentItem(DEFAULT_SELECTED_MENU_ITEM_POSITION);
//        如果不调用此方法 那么首次第一个fragment不显示
        switchFragment(mBottomNavigationViewEx.getMenu().getItem(DEFAULT_SELECTED_MENU_ITEM_POSITION).getItemId());
    }

    private void switchFragment(int menuItemId) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment targetFragment;
        int targetMenuItemPosition = mBottomNavigationViewEx.getMenuItemPosition(mBottomNavigationViewEx.getMenu().findItem(menuItemId));
        if (mMenuItemPagerMapping.indexOfKey(menuItemId) < 0) {
            targetFragment = createFragmentBasedOnMenuItemId(menuItemId);
            transaction.add(R.id.content_page_container, targetFragment);
        } else {
            targetFragment = mMenuItemPagerMapping.get(menuItemId);
        }
        if (mCurrentShownFragment == targetFragment) {
            return;
        }
        if (mCurrentShownFragment != null) {
            if (mCurrentMenuItemPosition < targetMenuItemPosition) {
                transaction.setCustomAnimations(R.anim.page_slide_right_in, R.anim.page_slide_left_out, 0, 0);
            } else {
                transaction.setCustomAnimations(R.anim.page_slide_left_in, R.anim.page_slide_right_out, 0, 0);
            }
            mCurrentShownFragment.setUserVisibleHint(false);
            transaction.hide(mCurrentShownFragment);
        }
        targetFragment.setUserVisibleHint(true);
        transaction.show(targetFragment);
        mCurrentMenuItemPosition = targetMenuItemPosition;
        mCurrentShownFragment = targetFragment;
        mMenuItemPagerMapping.put(menuItemId, targetFragment);
        transaction.commit();
    }

    private Fragment createFragmentBasedOnMenuItemId(int menuItemId) {
        Fragment fragment = null;
        switch (menuItemId) {
            case R.id.home: {
                fragment = new HomeFragment();
                break;
            }
            case R.id.guess: {
                fragment = new GuessFragment();
                break;
            }
            case R.id.about: {
                fragment = new AboutFragment();
                break;
            }
            default: {
                break;
            }
        }
        return fragment;
    }
}

Demo下载地址

 类似资料: