Fragment

来源:互联网 发布:半自动咖啡机推荐 知乎 编辑:程序博客网 时间:2024/06/07 20:44
package com.bwie.asus.project_two.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.RadioButton;import android.widget.RadioGroup;import com.bwie.asus.project_two.R;import com.bwie.asus.project_two.fragment.Fragment01;import com.bwie.asus.project_two.fragment.Fragment02;import com.bwie.asus.project_two.fragment.Fragment03;import com.bwie.asus.project_two.fragment.Fragment04;import com.bwie.asus.project_two.fragment.Fragment05;public class SecondActivity extends AppCompatActivity {    private RadioGroup radio;    private RadioButton bt1;    private FragmentManager manager;    private Fragment01 fragment01;    private Fragment02 fragment02;    private Fragment03 fragment03;    private Fragment04 fragment04;    private Fragment05 fragment05;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);        //查找控件        radio = (RadioGroup) findViewById(R.id.radio);        bt1 = (RadioButton) findViewById(R.id.bt1);        manager = getSupportFragmentManager();        if (bt1.isChecked()){            FragmentTransaction transaction = manager.beginTransaction();            fragment01 = new Fragment01();            transaction.add(R.id.frame, fragment01);            transaction.commit();            System.out.println("fragment01 = " + fragment01);        }        initData();    }    private void initData() {        //RadioGroup的监听事件        radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {                hideFragment();                switch (i){                    case R.id.bt1:                        FragmentTransaction transaction1 = manager.beginTransaction();                        if (fragment01 == null){                            fragment01 = new Fragment01();                            transaction1.add(R.id.frame, fragment01);                        }else{                            transaction1.show(fragment01);                        }                        transaction1.commit();                        break;                    case R.id.bt2:                        FragmentTransaction transaction2 = manager.beginTransaction();                        if (fragment02 == null) {                            fragment02 = new Fragment02();                            transaction2.add(R.id.frame, fragment02);                        }else {                            transaction2.show(fragment02);                        }                        transaction2.commit();                        break;                    case R.id.bt3:                        FragmentTransaction transaction3 = manager.beginTransaction();                        if (fragment03 == null) {                            fragment03 = new Fragment03();                            transaction3.add(R.id.frame, fragment03);                        }else{                            transaction3.show(fragment03);                        }                        transaction3.commit();                        break;                    case R.id.bt4:                        FragmentTransaction transaction4 = manager.beginTransaction();                        if (fragment04 == null) {                            fragment04 = new Fragment04();                            transaction4.add(R.id.frame, fragment04);                        }else{                            transaction4.show(fragment04);                        }                        transaction4.commit();                        break;                    case R.id.bt5:                        FragmentTransaction transaction5 = manager.beginTransaction();                        if (fragment05 == null) {                            fragment05 = new Fragment05();                            transaction5.add(R.id.frame, fragment05);                        }else{                            transaction5.show(fragment05);                        }                        transaction5.commit();                        break;                }            }        });    }    public void hideFragment(){        FragmentTransaction transaction1 = manager.beginTransaction();        if (fragment01 != null){            transaction1.hide(fragment01);        }if (fragment02 != null){            transaction1.hide(fragment02);        }        if (fragment03 != null){            transaction1.hide(fragment03);        }if (fragment04 != null){            transaction1.hide(fragment04);        }        if (fragment05 != null){            transaction1.hide(fragment05);        }        transaction1.commit();    }

}

<?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="com.bwie.asus.project_two.activity.SecondActivity">    <FrameLayout        android:id="@+id/frame"        android:layout_weight="1"        android:layout_width="match_parent"        android:layout_height="match_parent">    </FrameLayout>    <RadioGroup        android:id="@+id/radio"        android:layout_width="match_parent"        android:layout_height="80dp"        android:orientation="horizontal"        >        <RadioButton            android:id="@+id/bt1"            android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:button="@null"            android:checked="true"            android:background="@drawable/shape01"            />        <RadioButton            android:id="@+id/bt2"            android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:button="@null"            android:background="@drawable/shape02"            />        <RadioButton            android:id="@+id/bt3"            android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:button="@null"            android:background="@drawable/shape03"            />        <RadioButton            android:id="@+id/bt4"            android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:button="@null"            android:background="@drawable/shape04"            />        <RadioButton            android:id="@+id/bt5"            android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:button="@null"            android:background="@drawable/shape05"            />    </RadioGroup></LinearLayout>


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 系统装到f盘了怎么办 虚拟机占c盘内存怎么办 外机连无线虚拟机显示受限怎么办 使用msdn下载解压后怎么办 路由80端口被占用怎么办 c盘拒绝粘贴文件怎么办 oracle数据库密码忘了怎么办 电脑开机时不显示用户名怎么办? xp系统忘记开机密码怎么办 电脑开机密码忘了怎么办 c盘满了怎么办win10 win10电脑开机密码忘了怎么办 win10的开机密码忘了怎么办 u盘中了exe病毒怎么办 眼睛长个麦粒豆怎么办 苹果手机sdk授权失败怎么办 小米5王者荣耀卡怎么办 华为p9手机电池不耐用怎么办 华为g9青春版耗电快怎么办 华为手机摄像头坏了怎么办 华为p10摄像头玻璃划痕怎么办? 华为g9手机音量小怎么办 华为7pius太卡怎么办 华为畅享7plus卡怎么办 华为p9屏幕进水变颜色怎么办? 玩王者荣耀卡退怎么办 华为手机忘记开机密码怎么办 华为p9密码忘了怎么办 华为p9解锁密码忘了怎么办 华为手机不支持联通4g怎么办 酷派手机自动下载软件怎么办 小米5a不能开机怎么办 华为手机的视频找不到了怎么办 华为p8内存文件打不开怎么办 华为荣耀9网速慢怎么办 荣耀9手机网速慢怎么办 联想台式机不支持xp驱动怎么办 一体机尾插坏了怎么办 华为手机触屏失灵怎么办 华为p8max手机老是卡怎么办 手机屏碎了数据怎么办