安卓动态添加隐藏碎片简单用法

安卓动态添加隐藏碎片简单用法

欢迎扫码关注微信公众号"野心与家"

首先新建两个碎片:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".myFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/fragment1" />

</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".myFragment2">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/fragment2" />

</FrameLayout>
在主活动添加两个按钮:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity4">

    <!--    静态添加Fragment,在布局文件中以控件的形式添加-->
    <!--    注意:id属性必须设置,name属性必须设置,需要使用完整的包名-->

    <Button
        android:id="@+id/btn_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="152dp"
        android:layout_marginTop="800dp"
        android:layout_marginEnd="152dp"
        android:layout_marginBottom="100dp"
        android:text="添加碎片"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_hide"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fragment"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btn_hide"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="152dp"
        android:layout_marginEnd="152dp"
        android:layout_marginBottom="50dp"
        android:text="隐藏碎片"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_add"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.816" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraint"
        android:layout_width="470dp"
        android:layout_height="259dp"
        android:layout_marginStart="480dp"
        android:layout_marginTop="56dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
在主活动程序按钮注册一个点击事件

 		Button btn_add = (Button) findViewById(R.id.btn_add);
        Button btn_hide = (Button) findViewById(R.id.btn_hide);
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
                public void onClick(View view) {
//                btn_add.setText("我单击了该按钮");
                myFragment2 fragment2 =new myFragment2();//创建一个
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();//开启一个事务
//                transaction.add(R.id.constraint,fragment2);
                transaction.replace(R.id.constraint,fragment2);//在一个容器中替换一个碎片,如果碎片不存在则新建一个碎片,如果碎片存在则替换(建议使用)
                transaction.commit();
            }
        });
        btn_hide.setOnClickListener(new View.OnClickListener() {
            @Override
                public void onClick(View view) {
//                btn_hide.setText("我单击了该按钮");
                FragmentManager fragmentManager = getSupportFragmentManager();
                myFragment fragment1 = (myFragment) 			         
                fragmentManager.findFragmentById(R.id.fragment);//通过id属性引用布局文件的存在的碎片
                FragmentTransaction transaction1 = fragmentManager.beginTransaction();
                transaction1.hide(fragment1);//隐藏
                transaction1.commit();
            }	
        });

总结:

在上述代码中调用getSupportFragmentManager();方法得到FragmentManager

通过调用beginTransaction()方法开启一个事物

使用replace()向容器内动态添加或替换碎片

使用hide()向隐藏碎片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值