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>
阅读全文
0 0
- Fragment
- Fragment
- fragment
- Fragment
- Fragment
- Fragment
- Fragment
- fragment
- fragment
- Fragment
- Fragment
- fragment
- Fragment
- Fragment
- Fragment
- Fragment
- Fragment
- fragment
- [Ubuntu]虚拟机VirtualBox安装win7完整步骤
- 未优化的冒泡排序算法.c
- CS 400 8 Divisible 同余+模拟
- java字符串预习
- 原型链详解
- Fragment
- centos7.4编译reids4.0.2出现MALLOC=jemalloc错误
- linux进程地址空间
- 展讯平台android display驱动分析
- java注解
- 使用BottomTabBar实现底部导航,MVP网络框架,retrofit解析
- 洛谷 [P1403] 约数研究
- 在debian9上安装citrix receiver登录citrix虚拟桌面
- 优化后的排序冒泡排序算法.c