FrameLayout与底部导航栏的搭配使用

FrameLayout与底部导航栏的搭配使用

关于我

方法一:ViewPager与底部导航栏的搭配使用

方法二:FrameLayout与底部导航栏的搭配使用(底部导航栏使用了第三方依赖库)

可点击底部导航栏切换界面
为了快速编写代码。底部导航栏使用了第三方依赖库

    implementation 'me.majiajie:pager-bottom-tab-strip:2.2.5'

activity_home.xml

 <me.majiajie.pagerbottomtabstrip.PageNavigationView
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:layout_alignParentBottom="true"
        android:elevation="4dp" />
<FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/tab"
        android:layout_below="@id/ly_home_top">

</FrameLayout>

HomeActivity.java
声明变量:

    private FragmentManager fragmentManager;
    private NavigationController mNavigationController;
    private PageNavigationView pageBottomTabLayout;

initView()

 private void initViews() {
        signFragment = new SignFragment();
        educationFragment = new EducationFragment();
        communityFragment = new CommunityFragment();
        settingFragment = new SettingFragment();
        fragmentManager = getSupportFragmentManager();
        //设置默认显示的Fragment
       ragmentTransaction  fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl_content, signFragment);
        fragmentTransaction.commit();
    }

initTab()

 private void initTab() {
        pageBottomTabLayout = (PageNavigationView) findViewById(R.id.tab);
        
 mNavigationController = pageBottomTabLayout.material()
                .addItem(R.drawable.home, "主页", getResources().getColor(R.color.light_blue))
                .addItem(R.drawable.education, "教务", getResources().getColor(R.color.tanedu))
                .addItem(R.drawable.community, "交流", getResources().getColor(R.color.ori_main_color))
                .addItem(R.drawable.setting, "设置", 0xFF455A64)
                .setDefaultColor(0x89FFFFFF)//未选中状态的颜色
                .setMode(MaterialMode.CHANGE_BACKGROUND_COLOR | MaterialMode.HIDE_TEXT)//这里可以设置样式模式,总共可以组合出4种效果
                .build();

//设置Item选中事件的监听
        mNavigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
               FragmentTransaction  fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.remove(signFragment);
                switch (index) {
                    case 0:
                        fragmentTransaction.replace(R.id.fl_content, signFragment).commit();
                        break;
                    case 1:
                        fragmentTransaction.replace(R.id.fl_content, educationFragment).commit();
                        break;
                    case 2:
                        fragmentTransaction.replace(R.id.fl_content, communityFragment).commit();
                        break;
                    case 3:
                        fragmentTransaction.replace(R.id.fl_content, settingFragment).commit();
                        break;
                }
            }

            @Override
            public void onRepeat(int index) {
            }
        });

这样就行了。如果有什么问题。可以加工作QQ:3131713955

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值