Fragment的两种添加方法:静态&动态(附踩坑)

首先是静态添加fragment
创建相应的fragment文件继承Fragment,
查阅源码可以看出通过inflate方法来将layoutresource(布局资源)放入container(容器)中,

public View inflate(int resource, @Nullable ViewGroup root)

返回view即可将fragment中的内容通过fragment在Main_Activity文件中进行匹配设置,进而将fragment中的内容显示出来。

//继承自Fragment
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);//将layoutresource(布局资源)放入container(容器)中
        return view;
    }
}

在上述代码使用中,会出现以下报错:
The specified child already has a parent. 
You must call removeView() on the child's parent first.
说孩子已经有一个父辈了,说明我们已经添加过了,此时是出现了重复添加。

让我们看下下面这行代码:
 View view = inflater.inflate(R.layout.second_layout,container,false);

在这里插入图片描述
如图所示,attachToRoot这个参数表示是否添加到布局中去,而当我们没有写的时候是默认为true的,即自动添加到了布局中,添加对应布局不需要自动添加,因此在这里我们都将这个参数设置成false,这样一来就解决了这个问题。

和上文的代码类似,就是在通过inflater添加布局,而在参数设置中,
container之后多了一个false,就完美地解决了上述的报错

fragment布局,很简单只有一个TextView和一个Button

<?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"
    android:background="#f000">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment"
        android:gravity="center"/>

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

Main_Activity布局配置,在Fragment中进行匹配来显示想要得到的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">

    <TextView
        android:layout_width="193dp"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <fragment
        android:id="@+id/fragment"
        android:name="com.game.newfragmenttest.Fragment"//在此进行fragment的匹配
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    

</LinearLayout>

接下来是通过动态添加,用到replace这个方法

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //定义获取FragmentManager
        FragmentManager fragmentManager = getSupportFragmentManager();
        //使用FragmentTransaction
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //获取Fragment
        Fragment fragment =new Fragment();
        //利用fragmentTransaction的replace()方法,将fragment放入对应的framelayout中
        fragmentTransaction.replace(R.id.fm,fragment);
        //通过commit进入fragment生命周期进行操作
        fragmentTransaction.commit();
    }
}
使用FragmentManager的配置过程:
FragmentManager fragmentManager = getSupportFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.add(mContainerId, mFragment);

以下是在布局中添加一个FrameLayout来显示动态添加的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">

    <TextView
        android:layout_width="193dp"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/fm"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值