Android片段从静态替换为动态

问题描述 投票:0回答:1

我需要从Main活动中嵌入xml的静态片段传递到通过代码Java生成的另一个片段。因此任务是:1)使用嵌入的Fragment_one加载主要活动2)片段一具有一个按钮,以便传递到片段2。

Activity_main.xml

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

<fragment
    android:id="@+id/fragment_one"
    android:name="com.example.fragmentbookexample.FragmentOneLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout="@layout/fragment_one_layout"
    />


</androidx.constraintlayout.widget.ConstraintLayout>

Main_activity.java

package com.example.fragmentbookexample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends FragmentActivity {
    private Button b;
    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        b = findViewById (R.id.button);
    b.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick ( View v ) {
            FragmentTwoLayout second = new FragmentTwoLayout ();
            second.setArguments (getIntent ().getExtras ());

            FragmentManager fm = getSupportFragmentManager ();
            FragmentTransaction trans = fm.beginTransaction ();
            trans.replace (R.id.fragment_one, second);
            trans.addToBackStack (null);
            trans.commit ();
        }
    });
    }

}

Fragment_one_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".FragmentOneLayout"
    android:background="@color/colorAccent">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Static Fragment XML"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:layout_centerVertical="true"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="219dp"
        android:text="Pass to frag 2" />

</RelativeLayout>

Fragment_two_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".FragmentTwoLayout"
    android:background="@color/colorPrimaryDark">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Dynamic Fragment JAVA"
        android:textColor="#ffffff"
        android:gravity="center"/>

</RelativeLayout>

两个Java代码片段都是使用new-> fragment-> fragment(blank)自动创建的。

问题是,为什么当我用frag2替换frag1时,我看到不在frag2中但包含在frag1中的按钮?我认为替换效果很好,因为背景颜色会发生变化

enter image description here

enter image description here

提前感谢

android fragment
1个回答
0
投票

您不能在与FragmentTransaction一起使用的片段容器上执行任何<fragment>

如果使用AndroidX Fragment 1.2.0或更高版本,则应使用切换到使用1.2.0而不是FragmentContainerView标记,该标记确实支持与<fragment>一起使用:

FragmentTransaction
© www.soinside.com 2019 - 2024. All rights reserved.