美文网首页
Android底部导航栏BottomBar

Android底部导航栏BottomBar

作者: 我挺平凡 | 来源:发表于2019-12-10 14:19 被阅读0次

一、添加依赖

    //BottomBar
    implementation 'com.roughike:bottom-bar:2.3.1'

二、创建一个res/xml/bottombar_tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<tabs>
    <tab
        id="@+id/tab_home"
        icon="@drawable/ic_tab_home"
        title="@string/main_home" />
    <tab
        id="@+id/tab_category"
        icon="@drawable/ic_tab_category"
        title="@string/main_category" />
    <tab
        id="@+id/tab_cart"
        icon="@drawable/ic_tab_cart"
        title="@string/main_shopping_cart"/>
    <tab
        id="@+id/tab_mine"
        icon="@drawable/ic_tab_mine"
        title="@string/main_mine" />
</tabs>

三、activity_main.xml 中

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.MainActivity">

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.roughike.bottombar.BottomBar
        android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/size_54"
        android:background="@android:color/white"
        app:bb_activeTabColor="@color/colorFDD835"
        app:bb_inActiveTabColor="@color/color737373"
        app:bb_showShadow="false"
        app:bb_tabXmlResource="@xml/bottombar_tabs" />


    <!--app:bb_activeTabColor   选中的Tab颜色-->
    <!--bb_inActiveTabColor     未选中的Tab颜色-->
    <!--bb_showShadow           是否是平板模式-->
    <!--bb_tabXmlResource       tab的xml文件-->
    <!--bb_badgeBackgroundColor 设置Badges的背景色,就是右上角显示数字那个-->
    <!--bb_badgesHideWhenActive 小红点是否隐藏,默认为 true 隐藏。就是右上角显示数字那个-->
</LinearLayout>

四、MainActivity代码

package com.example.demo.ui;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.demo.R;
import com.example.demo.base.BaseActivity;
import com.example.demo.presenter.HomrPresenter;
import com.example.demo.ui.cart.fragment.CartFragment;
import com.example.demo.ui.category.fragment.CategoryFragment;
import com.example.demo.ui.home.fragment.HomeFragment;
import com.example.demo.ui.mine.fragment.MineFragment;
import com.example.demo.view.HomeView;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;

import butterknife.BindView;

public class MainActivity extends BaseActivity<EmptyPresenter, EmptyView> implements OnTabSelectListener {

    @BindView(R.id.bottom_bar)
    BottomBar bottomBar;
    private HomeFragment mHomeFragment;
    private CategoryFragment mCategoryFragment;
    private CartFragment mCartFragment;
    private MineFragment mMineFragment;

    // 当前正在显示的Fragment
    private Fragment mCurrentFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected EmptyPresenter initPresenter() {
        return new EmptyPresenter();
    }

    @Override
    protected void initView() {
        // "内存重启"时(例如修改手机字体大小), 恢复之前的Fragment.
        // 注意此方法只有在父类的onCreate(Bundle)调用之后才有效.
        retrieveFragments();
        bottomBar.setOnTabSelectListener(this);
    }

    @Override
    public void onBackPressed() {
        if (mCurrentFragment != mHomeFragment) {
            // 如果不是在HomeFragment, 则按返回键回到HomeFragment
            bottomBar.selectTabWithId(R.id.tab_home);
            return;
        }
        // 将Activity所在的Task移到后台, 而不是finish此Activity
        moveTaskToBack(true);
    }

    /**
     * 首页4个Fragment切换, 使用hide和show, 而不是replace.
     *
     * @param target 要显示的目标Fragment.
     */
    private void switchFragments(Fragment target) {
        if (mCurrentFragment == target) return;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mCurrentFragment != null) {
            // 隐藏当前正在显示的Fragment
            transaction.hide(mCurrentFragment);
        }
        if (target.isAdded()) {
            // 如果目标Fragment已经添加过,就显示它
            transaction.show(target);
        } else {
            // 否则直接添加该Fragment
            transaction.add(R.id.fl_container, target, target.getClass().getName());
        }
        transaction.commit();
        mCurrentFragment = target;
    }

    /**
     * 找回FragmentManager中存储的Fragment
     */
    private void retrieveFragments() {
        FragmentManager manager = getSupportFragmentManager();
        mHomeFragment = (HomeFragment) manager.findFragmentByTag(HomeFragment.class.getName());
        mCategoryFragment = (CategoryFragment) manager.findFragmentByTag(CategoryFragment.class.getName());
        mCartFragment = (CartFragment) manager.findFragmentByTag(CartFragment.class.getName());
        mMineFragment = (MineFragment) manager.findFragmentByTag(MineFragment.class.getName());
    }

    /**
     * Tab的选中事件
     */
    @Override
    public void onTabSelected(int tabId) {
        switch (tabId) {
            case R.id.tab_home:// 首页
                if (mHomeFragment == null) mHomeFragment = HomeFragment.newInstance();
                switchFragments(mHomeFragment);
                break;
            case R.id.tab_category:// 分类
                if (mCategoryFragment == null) mCategoryFragment = CategoryFragment.newInstance();
                switchFragments(mCategoryFragment);
                break;
            case R.id.tab_cart:// 购物车
                if (mCartFragment == null) mCartFragment = CartFragment.newInstance();
                switchFragments(mCartFragment);
                break;
            case R.id.tab_mine:// 我的
                if (mMineFragment == null) mMineFragment = MineFragment.newInstance();
                switchFragments(mMineFragment);
                break;
            default:
                throw new UnsupportedOperationException("Illegal branch!");
        }
    }
}

相关文章

网友评论

      本文标题:Android底部导航栏BottomBar

      本文链接:https://www.haomeiwen.com/subject/jecggctx.html