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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章