第三方开源库之 BottomNavigation

简介

BottomNavigation 一个优秀的底部导航栏。

效果图

第三方开源库之 BottomNavigation_第1张图片
效果图

配置

  1. 在 app/build.gradle 中添加:
implementation 'com.ashokvarma.android:bottom-navigation-bar:2.1.0'

使用

  1. 布局文件



    

    

    

  1. java 代码
    BottomNavigationActivity.java
public class BottomNavigationActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar mNavigationBar;

    private InviteFragment mInviteFragment = HomeFragmentFactory.getInstance().getInviteFragment();
    private ActivityFragment mActivityFragment = HomeFragmentFactory.getInstance().getActivityFragment();
    private FoundFragment mFoundFragment = HomeFragmentFactory.getInstance().getFoundFragment();
    private MineFragment mMineFragment = HomeFragmentFactory.getInstance().getMineFragment();

    private Fragment currentFragment = new Fragment();
//    private TextBadgeItem mBadgeItem;
    private FragmentManager fm;


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

    @Override
    protected void setToolbar() {

    }

    @Override
    protected void initView() {
        setNavTitle(R.string.bottom_navigation);

        fm = getSupportFragmentManager();

        initBottomBar();

    }

    private void initBottomBar() {

        /**
         * 导航基础设置 包括按钮选中效果 导航栏背景色等
         */
        mNavigationBar.setTabSelectedListener(this)
                .setMode(BottomNavigationBar.MODE_FIXED)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
                .setActiveColor("#F5DF3E")//选中颜色
                .setInActiveColor("#000000")//未选中颜色
                .setBarBackgroundColor("#ffffff");//导航栏背景色
//        mBadgeItem = new TextBadgeItem()
//                .setBorderWidth(2)//Badge的Border(边界)宽度
//                .setBorderColor(Color.BLUE)//Badge的Border颜色
//                .setBackgroundColor(Color.RED)
//                .setTextColor(Color.BLACK)//文本颜色
//                .setGravity(Gravity.RIGHT| Gravity.TOP)//位置,默认右上角
//                .setAnimationDuration(2000)
//                .setHideOnSelect(true)//当选中状态时消失,非选中状态显示
//                .setText("9");

        setInvite();


    }

    /**
     * 切换fragment
     *
     * @param targetFragment
     * @return
     */
    private FragmentTransaction switchFragment(Fragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {
            // 第一次使用switchFragment()时currentFragment为null,所以要判断一下
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.fl_content, targetFragment, targetFragment.getClass().getName());
        } else {
            transaction.hide(currentFragment).show(targetFragment);
        }
        currentFragment = targetFragment;
        return transaction;
    }


    /**
     * 邀请
     */
    private void setInvite() {
        mNavigationBar.clearAll();
        switchFragment(mInviteFragment).commitNowAllowingStateLoss();
        mNavigationBar.addItem(new BottomNavigationItem(R.mipmap.icon_invite_nor,"邀约"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_activity_nor,"活动"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_find_nor,"发现"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_my_nor,"我的"))
                .setFirstSelectedPosition(0)
                .initialise();

    }

    /**
     * 活动
     */
    private void setActivity() {
        mNavigationBar.clearAll();
        switchFragment(mActivityFragment).commitNowAllowingStateLoss();
        mNavigationBar.addItem(new BottomNavigationItem(R.mipmap.icon_invite_nor,"邀约"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_activity_nor,"活动"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_find_nor,"发现"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_my_nor,"我的"))
                .setFirstSelectedPosition(1)
                .initialise();

    }

    /**
     * 发现
     */
    private void setFound() {
        mNavigationBar.clearAll();
        switchFragment(mFoundFragment).commitNowAllowingStateLoss();
        mNavigationBar.addItem(new BottomNavigationItem(R.mipmap.icon_invite_nor,"邀约"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_activity_nor,"活动"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_find_nor,"发现"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_my_nor,"我的"))
                .setFirstSelectedPosition(2)
                .initialise();

    }

    /**
     * 我的
     */
    private void setMine() {
        mNavigationBar.clearAll();
        switchFragment(mMineFragment).commitNowAllowingStateLoss();
        mNavigationBar.addItem(new BottomNavigationItem(R.mipmap.icon_invite_nor,"邀约"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_activity_nor,"活动"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_find_nor,"发现"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_my_nor,"我的"))
                .setFirstSelectedPosition(3)
                .initialise();

    }

    @Override
    public void onTabSelected(int position) {
        FragmentTransaction ft = fm.beginTransaction();
        switch (position) {
            case 0:
                setInvite();
                break;

            case 1:
                setActivity();
                break;

            case 2:
                setFound();
                break;

            case 3:
                setMine();
                break;
        }

    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
}

四个类似的Fragment , ActivityFragment.java

public class ActivityFragment extends BaseFragment {
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_activity;
    }
}

HomeFragmentFactory.java

/**
 * Created on 2019/12/2 16:44
 *
 * @author Gong Youqiang
 */
public class HomeFragmentFactory {
    static HomeFragmentFactory mInstance;
    private InviteFragment mInviteFragment;
    private ActivityFragment mActivityFragment;
    private FoundFragment mFoundFragment;
    private MineFragment mMineFragment;

    public HomeFragmentFactory() {
    }

    public static HomeFragmentFactory getInstance() {
        if (mInstance == null) {
            synchronized (HomeFragmentFactory.class) {
                if (mInstance == null) {
                    mInstance = new HomeFragmentFactory();
                }
            }
        }
        return mInstance;
    }

    /**
     * 邀请
     *
     * @return
     */
    public InviteFragment getInviteFragment() {
        if (mInviteFragment == null) {
            synchronized (InviteFragment.class) {
                if (mInviteFragment == null) {
                    mInviteFragment = new InviteFragment();
                }
            }
        }
        return mInviteFragment;
    }

    /**
     * 活动
     *
     * @return
     */
    public ActivityFragment getActivityFragment() {
        if (mActivityFragment == null) {
            synchronized (ActivityFragment.class) {
                if (mActivityFragment == null) {
                    mActivityFragment = new ActivityFragment();
                }
            }
        }
        return mActivityFragment;
    }

    /**
     * 发现
     *
     * @return
     */
    public FoundFragment getFoundFragment() {
        if (mFoundFragment == null) {
            synchronized (FoundFragment.class) {
                if (mFoundFragment == null) {
                    mFoundFragment = new FoundFragment();
                }
            }
        }
        return mFoundFragment;
    }

    /**
     * 我的
     *
     * @return
     */
    public MineFragment getMineFragment() {
        if (mMineFragment == null) {
            synchronized (MineFragment.class) {
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                }
            }
        }
        return mMineFragment;
    }


}

你可能感兴趣的:(第三方开源库之 BottomNavigation)