cardview

// 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/frame_content"></FrameLayout>

    <com.ashokvarma.bottomnavigation.BottomNavigationBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:elevation="5dp"
        android:id="@+id/bnb_main">
    </com.ashokvarma.bottomnavigation.BottomNavigationBar>

</LinearLayout>

//代码
public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {

    private BottomNavigationBar mBottomNavigationBar;
    private FragmentTransaction mTransaction;
    private FragmentOne mPlanFragment;
    private Fragment mCurrentFragment;
    private FragmentTwo mAccountFragment;
    private FragmentThree mNewsFragment;
    private FragmentFore mLifeFragment;


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

// 底部导航栏
        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bnb_main);
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_launcher,"规划")
                .setActiveColorResource(R.color.colorPrimaryDark))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher,"记账")
                        .setActiveColorResource(R.color.colorPrimary))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher,"新闻")
                        .setActiveColorResource(R.color.colorAccent))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher,"生活")
                        .setActiveColorResource(R.color.colorAccent))
                .setFirstSelectedPosition(0)
                .initialise();
// 设置默认页
        mTransaction = getSupportFragmentManager().beginTransaction();
        mPlanFragment = new FragmentOne();
        mTransaction.add(R.id.frame_content, mPlanFragment).commit();
        mCurrentFragment = mPlanFragment;

// 设置导航监听器
        mBottomNavigationBar.setTabSelectedListener(this);

}


    @Override
    public void onTabSelected(int position) {
        switch (position) {
            case 0:
                if (mPlanFragment == null) {
                    mPlanFragment = new FragmentOne();
                }
                switchFragment(mPlanFragment);
                break;

            case 1:
                if (mAccountFragment == null) {
                    mAccountFragment = new FragmentTwo();
                }
                switchFragment(mAccountFragment);
                break;

            case 2:
                if (mNewsFragment == null) {
                    mNewsFragment = new FragmentThree();
                }
                switchFragment(mNewsFragment);
                break;

            case 3:
                if (mLifeFragment == null) {
                    mLifeFragment = new FragmentFore();
                }
                switchFragment(mLifeFragment);
                break;

            default:

                break;
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }


    /**
     * 导航切换
     */
    private void switchFragment(Fragment fragment) {
        if (mCurrentFragment != fragment) {
            mTransaction = getSupportFragmentManager().beginTransaction();
            if (!fragment.isAdded()) {
                mTransaction.hide(mCurrentFragment).add(R.id.frame_content, fragment).commit();
            } else {
                mTransaction.hide(mCurrentFragment).show(fragment).commit();
            }
            mCurrentFragment = fragment;
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值