FrameLayout加RadioGroup实现底部点击切换

废话不多说直接上源码

主界面

MainActivity

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import jh.jinghui.base.BaseActivity;
import jh.jinghui.view.fragment.HaoYouFragment;
import jh.jinghui.view.fragment.MyFragment;
import jh.jinghui.view.fragment.ShouYeFragment;
import jh.jinghui.view.fragment.XiaoXiFragment;
import jh.jinghui.xyes.R;

/**
*@Class name MainActivity
*@Description: 主界面
*@Author Mr.JH
*@Time 2017/9/22
*/
public class MainActivity extends BaseActivity {
    //首页
    public static final String shouyeFragment = "shouyeFragment";
    //消息界面
    public static final String xiaoxiFragment = "xiaoxiFragment";
    //好友界面
    public static final String haoyouFragment = "haoyouFragment";
    //我的界面
    public static final String myFragment = "myFragment";
    private RadioGroup mRg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initDao(savedInstanceState);//底部导航栏点击事件
    }

    private void initView() {
        mRg = (RadioGroup) findViewById(R.id.main_rg);
    }

    /**
    *@Params :底部导航栏点击事件
    *@Author :Mr.JH
    *@Date :2017/9/22
     * @param savedInstanceState
    */
    private void initDao(Bundle savedInstanceState) {
        mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Fragment fragment1 = fm.findFragmentByTag(shouyeFragment);
                Fragment fragment2 = fm.findFragmentByTag(xiaoxiFragment);
                Fragment fragment3 = fm.findFragmentByTag(haoyouFragment);
                Fragment fragment4 = fm.findFragmentByTag(myFragment);
                if (fragment1!=null){
                    ft.hide(fragment1);
                }
                if (fragment2!=null){
                    ft.hide(fragment2);
                }
                if (fragment3!=null){
                    ft.hide(fragment3);
                }
                if (fragment4!=null){
                    ft.hide(fragment4);
                }
                switch (checkedId){
                    case R.id.rb_shouye:
                        if (fragment1==null){
                            fragment1= new ShouYeFragment();
                            ft.add(R.id.fl_main,fragment1,shouyeFragment);
                        }else {
                            ft.show(fragment1);
                        }
                        break;
                    case R.id.rb_xiaoxi:
                        if (fragment2==null){
                            fragment2= new XiaoXiFragment();
                            ft.add(R.id.fl_main,fragment2,xiaoxiFragment);
                        }else {
                            ft.show(fragment2);
                        }
                        break;
                    case R.id.rb_haoyou:
                        if (fragment3==null){
                            fragment3= new HaoYouFragment();
                            ft.add(R.id.fl_main,fragment3,haoyouFragment);
                        }else {
                            ft.show(fragment3);
                        }
                        break;
                    case R.id.rb_my:
                        if (fragment4==null){
                            fragment4= new MyFragment();
                            ft.add(R.id.fl_main,fragment4,myFragment);
                        }else {
                            ft.show(fragment4);
                        }
                        break;
                }
                ft.commit();
            }
        });
        if (savedInstanceState==null){
            FragmentManager fragmentManager = getSupportFragmentManager();
            Fragment fragment = new ShouYeFragment();
            fragmentManager.beginTransaction()
                    .replace(R.id.fl_main,fragment,shouyeFragment).commit();
        }
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onRestoreInstanceState(savedInstanceState, persistentState);
        for (int i = 0; i < mRg.getChildCount(); i++) {
            RadioButton mTab = (RadioButton) mRg.getChildAt(i);
            FragmentManager fm = getSupportFragmentManager();
            Fragment fragment = fm.findFragmentByTag((String) mTab.getTag());
            FragmentTransaction ft = fm.beginTransaction();
            if (fragment != null) {
                if (!mTab.isChecked()) {
                    ft.hide(fragment);
                }
            }
            ft.commit();
        }
    }
}

主界面布局

activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="jh.jinghui.xyes.jh.jinghui.view.MainActivity">

    <FrameLayout
        android:id="@+id/fl_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <include layout="@layout/main_radiogroup"/>

</LinearLayout>
main_radiogroup
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioGroup
        android:id="@+id/main_rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/appdibu"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rb_shouye"
            android:text="首页"
            android:checked="true"
            android:drawableTop="@drawable/selector_shouye"
            style="@style/RadioGroupStyle"/>
        <RadioButton
            android:id="@+id/rb_xiaoxi"
            android:text="消息"
            android:drawableTop="@drawable/selector_xiaoxi"
            style="@style/RadioGroupStyle"/>
        <RadioButton
            android:id="@+id/rb_haoyou"
            android:text="好友"
            android:drawableTop="@drawable/selector_haoyou"
            style="@style/RadioGroupStyle"/>
        <RadioButton
            android:id="@+id/rb_my"
            android:text="我的"
            android:drawableTop="@drawable/selector_my"
            style="@style/RadioGroupStyle"/>
    </RadioGroup>
</RelativeLayout>

四个fragment自己创建


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值