通过进出栈方式来对Fragment进行添加、移除

本文介绍了一种使用堆栈实现Fragment动态添加与移除的方法。通过创建Fragment布局与类,利用MainActivity中的按钮控制Fragment的增删。堆栈用于记录每个添加的Fragment,便于回退操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要通过堆栈的方式来实现Fragment的添加与移除
首先我们创建一个Fragment的布局,放一个TextView进去

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

创建Fragment文件继承于Fragment

package com.game.broadcast;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class Fragment extends androidx.fragment.app.Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment,container,false);
return view;
    }
}

在Main布局中添加Fragment,放入两个按钮

<?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=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>

    <fragment
        android:id="@+id/fragment"
        android:name="com.game.broadcast.Fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

接着在MainActivity中使用堆栈进行fragment的增减

package com.game.broadcast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Stack;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView textView;
    Button button1;
    Button button2;
    FragmentManager fragmentManager;
    //创建fragment的堆栈用于存放、删减
    Stack<Fragment>fragments=new Stack<Fragment>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1=(Button)findViewById(R.id.button);
        button2=(Button)findViewById(R.id.button2);
        button1.setText("add");
        button2.setText("remove");
        button1= (Button)findViewById(R.id.button);
        button2 = (Button)findViewById(R.id.button2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
}

    @Override
    public void onClick(View view) {

        Fragment fragment = new Fragment();
        FragmentManager fragmentManager =getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        switch(view.getId()){
            case R.id.button:
                fragmentTransaction.add(R.id.fragment,fragment);
                //将添加的fragment通过push放入堆栈
                this.fragments.push(fragment);
                fragmentTransaction.commit();
                break;
            case R.id.button2:
                //通过empty来判断堆栈是否为空,如果不为空则可以进行点击删掉fragment
                if(!this.fragments.empty())
                    //pop移除出栈
                fragmentTransaction.remove(this.fragments.pop());
                fragmentTransaction.commit();
                break;
                default:
                    break;
        }

    }
}

这里用到push将fragment加入堆栈中去,用pop出栈
用empty进行判定
根据empty的源码可以看出是一个boolean类型的,值应当是true或者false,

public boolean empty() {
        throw new RuntimeException("Stub!");
    }

因此在这里我们判断是否为空栈时也可以将代码写作
在这里插入图片描述
false即为不空,可以移除fragment,true则为空了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值