Fragment切换和隐藏,显示(show,hide)
来源:互联网 发布:c语言源码网站 编辑:程序博客网 时间:2024/06/07 05:14
代码演示XML<?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.bawei.zhangjinfeng20171105.MainActivity"> <FrameLayout android:id="@+id/fl" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout> <RadioGroup android:id="@+id/rg" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="30dp"> <RadioButton android:text="Frag1" android:textSize="40px" android:button="@null" android:id="@+id/ra1" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:text="Frag2" android:textSize="40px" android:button="@null" android:id="@+id/ra2" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:text="Frag3" android:textSize="40px" android:button="@null" android:id="@+id/ra3" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:text="Frag4" android:textSize="40px" android:button="@null" android:id="@+id/ra4" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </RadioGroup></LinearLayout>Java
package com.bawei.zhangjinfeng20171105;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.RadioGroup;import com.bawei.zhangjinfeng20171105.frag.Four;import com.bawei.zhangjinfeng20171105.frag.One;import com.bawei.zhangjinfeng20171105.frag.Three;import com.bawei.zhangjinfeng20171105.frag.Two;public class MainActivity extends AppCompatActivity { private FragmentManager fragmentManager; private RadioGroup rg; private FrameLayout fl; private One one; private Two two; private Three three; private Four four; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取管理者 fragmentManager = getSupportFragmentManager(); //初始化组件 fl = (FrameLayout) findViewById(R.id.fl); rg = (RadioGroup) findViewById(R.id.rg); //将所有Fragment添加到占位布局 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //实例化第一个Frangment one = new One();// fragmentTransaction.add(R.id.fl, One).commit(); fragmentTransaction.add(R.id.fl,one).commit(); /* RadioGroupd点击监听 */ rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { //隐藏所有Fragment hidefragment(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); switch (i){ case R.id.ra1: //展示第一个 fragmentTransaction.show(one).commit(); break; case R.id.ra2: //展示第二个 if(two==null){ two = new Two(); fragmentTransaction.add(R.id.fl, two).commit(); }else{ fragmentTransaction.show(two).commit(); } break; case R.id.ra3: //展示第三个 if(three==null){ three = new Three(); fragmentTransaction.add(R.id.fl, three).commit(); }else{ fragmentTransaction.show(three).commit(); } break; case R.id.ra4: //展示第三个 if(four==null){ four = new Four(); fragmentTransaction.add(R.id.fl, four).commit(); }else{ fragmentTransaction.show(four).commit(); } break; } } }); } private void hidefragment() { FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //如果Fragment不为空并且已经添加,就隐藏 if(one!=null&&one.isAdded()){ fragmentTransaction.hide(one); } if(two!=null&&two.isAdded()){ fragmentTransaction.hide(two); } if(three!=null&&three.isAdded()){ fragmentTransaction.hide(three); } if(four!=null&&four.isAdded()){ fragmentTransaction.hide(four); } //提交 fragmentTransaction.commit(); }}
阅读全文
0 0
- Fragment切换和隐藏,显示(show,hide)
- show hide 显示隐藏切换Fragment
- hide隐藏和show显示
- Fragment 通过show和hide 切换界面
- Fragment切换(show() and hide())
- jQuery hide() 和 show() 隐藏和显示
- 显示show()隐藏hide()
- Fragment show和hide
- Fragment Hide() Show()切换实例
- 隐藏显示控制(is/hide/show)
- 关于Activity加载Fragment切换Show和Hide重叠问题
- 【jQuery】调用show()和hide()方法显示和隐藏元素
- jquery show hide 显示和隐藏失效/不起作用
- Fragment切换 保存状态(add show hide)
- Fragment切换 hide/show 生命周期的运用
- Fragment通过hide、show实现动态切换
- android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态
- android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态
- mysql 触发器after与before的区别
- 内核对象介绍
- PAT乙级系列:第二题
- indexFor方法分析
- Bookshelf
- Fragment切换和隐藏,显示(show,hide)
- tomcat环境配置
- 运行时栈帧结构
- Retrofit+RxJava+Fresco简单实现数据展示
- java学习(十二)—并发编程(一)
- Mac 重置mysql数据库密码
- 4.2
- 视频
- java web整合开发 王者归来