tabbar页面切换(RadioButton+FragmentManager+FrameLayout)

tabbar文字切换样式:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#1cfa45"/>
    <item android:state_checked="false" android:color="#8b8b8b"/>
</selector>

tabbar按钮切换样式:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@mipmap/ic_wechat_checked"/>
    <item android:state_checked="false" android:drawable="@mipmap/ic_wechat_uncheck"/>
</selector>

activity布局:

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

    <FrameLayout
        android:layout_weight="1"
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#000"/>
    <RadioGroup
        android:orientation="horizontal"
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <RadioButton
            android:textColor="@drawable/color_text"
            android:gravity="center"
            android:drawableTop="@drawable/radio_wechat"
            android:button="@null"
            android:id="@+id/btn_wechat"
            android:text="微信"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
        <RadioButton
            android:textColor="@drawable/color_text"
            android:gravity="center"
            android:drawableTop="@drawable/radio_found"
            android:button="@null"
            android:id="@+id/btn_found"
            android:text="发现"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
        <RadioButton
            android:textColor="@drawable/color_text"
            android:gravity="center"
            android:drawableTop="@drawable/radio_contacts"
            android:button="@null"
            android:id="@+id/btn_contacts"
            android:text="联系人"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </RadioGroup>
</LinearLayout>

activity代码:


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.uoso.myapplicationdemo.R;

/**
 * fragment实现页面切换
 */
public class ActFragment extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private FrameLayout frameLayout;
    private RadioGroup radioGroup;
    private FragmentWechat fragmentWechat;
    private FragmentManager manager;

    private FragmentContacts fragmentContacts;
    private FragmentFound fragmentFound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_fragment);
        frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        //默认选中第一个
        RadioButton btn = (RadioButton) radioGroup.getChildAt(0);
        btn.setChecked(true);
        initFragment();
        radioGroup.setOnCheckedChangeListener(ActFragment.this);
    }

    /**
     * 初始化第一个页面
     */
    private void initFragment() {
        //获取管理器
        manager = getSupportFragmentManager();
        //通过管理器获取一个事件
        FragmentTransaction transaction = manager.beginTransaction();
        //添加第一个fragment到帧布局中
        fragmentWechat = new FragmentWechat();
        transaction.add(R.id.frame_layout, fragmentWechat);
        transaction.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        switch (i){
            case R.id.btn_wechat: //微信
                FragmentTransaction ft1 = manager.beginTransaction();
                hideAll(ft1);
                if (fragmentWechat ==null){
                    fragmentWechat = new FragmentWechat();
                    ft1.add(R.id.frame_layout, fragmentWechat);
                }else {
                    ft1.show(fragmentWechat);
                }
                ft1.commit();
                break;
            case R.id.btn_contacts://联系人
                FragmentTransaction ft2 = manager.beginTransaction();
                hideAll(ft2);
                if (fragmentContacts ==null){
                    fragmentContacts = new FragmentContacts();
                    ft2.add(R.id.frame_layout, fragmentContacts);
                }else {
                    ft2.show(fragmentContacts);
                }
                ft2.commit();
                break;
            case R.id.btn_found: //发现
                FragmentTransaction ft3 = manager.beginTransaction();
                hideAll(ft3);
                if (fragmentFound ==null){
                    fragmentFound = new FragmentFound();
                    ft3.add(R.id.frame_layout, fragmentFound);
                }else {
                    ft3.show(fragmentFound);
                }
                ft3.commit();
                break;
        }
    }

    /**
     * 隐藏所有fragment
     * @param ft
     */
    private void hideAll(FragmentTransaction ft){
        if (ft==null){
            return;
        }
        if (fragmentWechat !=null){
            ft.hide(fragmentWechat);
        }
        if (fragmentFound !=null){
            ft.hide(fragmentFound);
        }
        if (fragmentContacts !=null){
            ft.hide(fragmentContacts);
        }
    }
}

fragment布局:

<?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:background="#8bd8eb"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

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 com.example.uoso.myapplicationdemo.R;

/**
 * Created by UOSO on 2018/3/19.
 */

public class FragmentWechat extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wechat, container, false);
        return view;
    }
}

猜你喜欢

转载自blog.csdn.net/an_nal/article/details/80324134