android便于维护的下面选项卡状态切换

效果图


布局

  <LinearLayout
        android:id="@+id/main_bottome_switcher_container"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:src="@drawable/home" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:layout_gravity="bottom"
                android:gravity="center"
                android:text="首页"
                android:textColor="@color/main_bottom_tv_color" />

        </FrameLayout>
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:src="@drawable/order" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:layout_gravity="bottom"
                android:gravity="center"
                android:text="订单"
                android:textColor="@color/main_bottom_tv_color" />
        </FrameLayout>
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:src="@drawable/me" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:layout_gravity="bottom"
                android:gravity="center"
                android:text="个人"
                android:textColor="@color/main_bottom_tv_color" />
        </FrameLayout>
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:src="@drawable/more" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:layout_gravity="bottom"
                android:gravity="center"
                android:text="更多"
                android:textColor="@color/main_bottom_tv_color" />
        </FrameLayout>
    </LinearLayout>


代码

找到id后 onCreate中写这三个方法

        //点击底部4个选项卡 目的切换顶部真布局执行的fragment准备4geFragment
        initFragment();

        initClick();
        //默认选中索引位置为0的页面
        onClick(mainBottomeSwitcherContainer.getChildAt(0));
方法的具体实现

 private void initClick() {
        int childCount = mainBottomeSwitcherContainer.getChildCount();
        for (int i = 0; i < childCount; i++) {
            FrameLayout frameLayout = (FrameLayout) mainBottomeSwitcherContainer.getChildAt(i);
            frameLayout.setOnClickListener(this);
        }
    }

    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new OrderFragment());
        fragments.add(new UserFragment());
        fragments.add(new MoreFragment());
    }

    @Override
    public void onClick(View v) {
        //提供点中所在线性布局容器中的索引位置
        int indexOfChild = mainBottomeSwitcherContainer.indexOfChild(v);
        //切换选中FrameLayout内部孩子节点的颜色
        changeUI(indexOfChild);

        // 切换Fragment(首页 订单 用户  更多)
        changeFragment(indexOfChild);


    }

    private void changeFragment(int indexOfChild) {
        fragments.size();
        getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container,fragments.get(indexOfChild),indexOfChild+"").commit();
    }

    /**
     * 切换选中FrameLayout内部孩子节点的颜色
     *
     * @param indexOfChild
     */
    private void changeUI(int indexOfChild) {

        int childCount = mainBottomeSwitcherContainer.getChildCount();
        for (int i = 0; i < childCount; i++) {
            //获取每一个孩子节点,如果选中索引和孩子节点索引一致,则设置控件为不可用,不可用对应蓝色效果
            FrameLayout frameLayout = (FrameLayout)mainBottomeSwitcherContainer.getChildAt(i);
            if (i == indexOfChild) {
                //找到了选中的对象  以及孩子都需要要变成蓝色
                setEnable(frameLayout, false);
            } else {
                // 没找到的 不需要变色
                setEnable(frameLayout, true);
            }
        }
    }

    /**
     * @param view
     * @param b
     */
    private void setEnable(View view, boolean b) {
        //父布局操作
        view.setEnabled(b);
        //view 是否能变成viewGroup判断
        if (view instanceof ViewGroup) {
            int childCount = ((ViewGroup) view).getChildCount();
            for (int i = 0; i < childCount; i++) {
                View viewchild = ((ViewGroup) view).getChildAt(i);
//                viewchild.setEnabled(b);
                //更高级的写法 递归调用 这样写的好处是 如果除了这两个子类还有子类的话 还能继续往下设置
                setEnable(viewchild, b);
            }
        }

    }
对了 这里的selector 用的是state_enable属性

home.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/home_normal" android:state_enabled="true"/>
    <item android:drawable="@drawable/home_disabled" android:state_enabled="false"/>
</selector>
main_bottom_tv_color

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#000" android:state_enabled="true"/>
    <item android:color="#28b2ed" android:state_enabled="false"/>
</selector>








  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值