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();    }}


原创粉丝点击