Android 底部菜单栏RadioButton+Fragment

1,主页面

<?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="com.sky.com.vegetables.MainActivity">
    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

    </FrameLayout>
    <RadioGroup
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#fff"
        android:gravity="center"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/home_rb"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="首页" />

        <RadioButton
            android:id="@+id/my_rb"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"

            android:text="我的" />
        <!--  android:textColor="@drawable/select_font_color"
            android:drawableTop="@drawable/select_tab_my"-->
    </RadioGroup>
</LinearLayout>

2,两个Fragment XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="我的"
        android:textColor="@color/colorPrimary"
        />
</LinearLayout>

3,Main 代码

package com.sky.com.vegetables;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.sky.com.vegetables.fragment.HomeFragment;
import com.sky.com.vegetables.fragment.MyFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    @Bind(R.id.fragment)
    FrameLayout mFragment;
    @Bind(R.id.home_rb)
    RadioButton mHome;
    @Bind(R.id.my_rb)
    RadioButton mMy;
    @Bind(R.id.tabs)
    RadioGroup mTabs;
    private Fragment mHomeFragment, mMyFragment//两个需要显示的页面
    private  Fragment  isFragment;// 当前显示的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.fragment, mHomeFragment).commit();
        isFragment = mHomeFragment;

    }

    private void initView() {
        mTabs.setOnCheckedChangeListener(this);
       //添加多个的话就在这new,下面点击事件,也写上。
        mHomeFragment = new HomeFragment();
        mMyFragment = new MyFragment();
    }
    //点击切换。
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        switch (i) {
            case R.id.home_rb:

                switchFragment(mHomeFragment);
                break;
            case R.id.my_rb:
                switchFragment(mMyFragment);
                break;


        }
    }

    private void switchFragment(Fragment fragment) {
        //判断当前显示的Fragment是不是切换的Fragment
        if (isFragment != fragment) {
            //判断切换的Fragment是否已经添加过
            if (!fragment.isAdded()) {
                //如果没有,则先把当前的Fragment隐藏,把切换的Fragment添加上
                getSupportFragmentManager().beginTransaction().hide(isFragment)
                        .add(R.id.fragment, fragment).commit();
            } else {
                //如果已经添加过,则先把当前的Fragment隐藏,把切换的Fragment显示出来
                getSupportFragmentManager().beginTransaction().hide(isFragment).show(fragment).commit();
            }
            isFragment = fragment;
        }
    }
}

4,Fragment 代码

package com.sky.com.vegetables.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.sky.com.vegetables.R;

/**
 * Created by ${CWJ} on 2017/7/27.
 * 描述: 个人中心
 */

public class MyFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_my,container,false);
        //初始化 什么的 放这里。
        return v;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值