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

来源:互联网 发布:jquery rotate.js cdn 编辑:程序博客网 时间:2024/06/01 08:42

效果图


布局

  <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>








0 0
原创粉丝点击