美文网首页
radiogroup + fragment实现页面切换

radiogroup + fragment实现页面切换

作者: 强0808 | 来源:发表于2018-06-28 09:54 被阅读0次

Activity布局


<?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"
    tools:context=".ui.RadioGroupActivity">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>

    <RadioGroup
        android:id="@+id/rgTools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@mipmap/nav_footer_white"
        android:gravity="center"
        android:orientation="horizontal">


        <RadioButton
            android:id="@+id/rbHome"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:button="@null"
            android:checked="true"
            android:drawableTop="@drawable/selector_main_btn_home"
            android:gravity="center"
            android:paddingLeft="0dp"
            android:text="首页" />

        <RadioButton
            android:id="@+id/rbShop"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:checked="false"
            android:drawableTop="@drawable/selector_main_btn_shopcart"
            android:gravity="center"
            android:text="购物车" />

        <RadioButton
            android:id="@+id/rbMessage"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:checked="false"
            android:drawableTop="@drawable/selector_main_btn_message"
            android:gravity="center"
            android:text="消息" />

        <RadioButton
            android:id="@+id/rbMine"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:button="@null"
            android:checked="false"
            android:drawableTop="@drawable/selector_main_btn_mine"
            android:gravity="center"
            android:text="我的" />

    </RadioGroup>


</LinearLayout>

Activity主要逻辑处理

    private Fragment[] mFragments;
    private int mIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_group);
        ButterKnife.bind(this);
        initFragment();
    }

    private void initFragment() {
        //首页
        HomeFragment homeFragment = new HomeFragment();
        //购物车
        ShopcartFragment shopcartFragment = new ShopcartFragment();

        //消息
        MessageFragment messageFragment = new MessageFragment();
        //个人中心

        MineFragment mineFragment = new MineFragment();

        //添加到数组
        mFragments = new Fragment[]{homeFragment, shopcartFragment, messageFragment, mineFragment};

        //开启事务
        FragmentTransaction ft =
                getSupportFragmentManager().beginTransaction();

        //添加首页
        ft.add(R.id.content, homeFragment).commit();

        //默认设置为第0个
        setIndexSelected(0);
    }

    private void setIndexSelected(int index) {

        if (mIndex == index) {
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        //隐藏
        ft.hide(mFragments[mIndex]);
        //判断是否添加
        if (!mFragments[index].isAdded()) {
            ft.add(R.id.content, mFragments[index]).show(mFragments[index]);
        } else {
            ft.show(mFragments[index]);
        }

        ft.commit();
        //再次赋值
        mIndex = index;

    }

    @OnClick({R.id.rbHome, R.id.rbShop, R.id.rbMessage, R.id.rbMine, R.id.rgTools})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rbHome:
                setIndexSelected(0);
                break;
            case R.id.rbShop:
                setIndexSelected(1);
                break;
            case R.id.rbMessage:
                setIndexSelected(2);
                break;
            case R.id.rbMine:
                setIndexSelected(3);
                break;
        }
    }

借鉴
https://www.cnblogs.com/liunanjava/p/5814873.html

相关文章

网友评论

      本文标题:radiogroup + fragment实现页面切换

      本文链接:https://www.haomeiwen.com/subject/odwwyftx.html