MaterialButtonToggleGroup 做底部导航栏和FragmentContainerView联动

nice~

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:ignore="MissingConstraints">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fcv_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/teal_200"
        android:tag="main"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottom"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation"

        />


        <com.google.android.material.button.MaterialButtonToggleGroup
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            app:singleSelection="true"
            app:layout_constraintBottom_toBottomOf="parent">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_main"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/button_bottom_navi_style"

                android:text="首页" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_moments"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"

                style="@style/button_bottom_navi_style"

                android:text="萌圈" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_add"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/button_bottom_navi_style"
                android:text="+" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_shop"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/button_bottom_navi_style"
                android:text="商城" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_me"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/button_bottom_navi_style"
                android:text="我的" />
        </com.google.android.material.button.MaterialButtonToggleGroup>



</androidx.constraintlayout.widget.ConstraintLayout>
<navigation 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:id="@+id/trees_of_block"
    tools:ignore="UnusedNavigation"
    app:startDestination="@id/btn_main">
    <fragment
        android:id="@+id/btn_main"
        android:name="com.vily.home.fragment.MainFragment"
        android:label="four"
        tools:layout="@layout/fragment_main"/>
    <fragment
        android:id="@+id/btn_moments"
        android:name="com.vily.home.fragment.MomentsFragment"
        android:label="one"
        tools:layout="@layout/fragment_moments"/>
    <fragment
        android:id="@+id/btn_shop"
        android:name="com.vily.home.fragment.ShopFragment"
        android:label="two"
        tools:layout="@layout/fragment_shop"/>
    <fragment
        android:id="@+id/btn_me"
        android:name="com.vily.home.fragment.MeFragment"
        android:label="three"
        tools:layout="@layout/fragment_me"/>
</navigation>

        mInflate.bottom.addOnButtonCheckedListener((group, checkedId, isChecked) -> {

            if(isChecked && defalutCheckId!=checkedId) {
                Log.i(TAG, "initListener: ---------xxxx");
                if(checkedId==R.id.btn_add){
                    Log.i(TAG, "initListener: --------弹出框");

                }else{
                    mNavController.navigate(checkedId);
                }

                group.findViewById(defalutCheckId).setSelected(false);
                group.findViewById(checkedId).setSelected(true);
                defalutCheckId = checkedId;
            }

        });
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fcv_fragment);
mNavController = navHostFragment.getNavController();

defalutCheckId=R.id.btn_main;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值