首页 > 解决方案 > 单击浮动操作按钮时,片段与另一个片段重叠

问题描述

我正在尝试在片段中打开另一个片段。但是,当我单击 fab 时,我看到布局已被替换,但之前的片段仍保留在那里。我尝试替换配置文件 XML 的整个布局,但它没有正确替换。我试图将其称为意图,但我不能将片段称为活动。

片段:

    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private FloatingActionButton fab;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_profile, container, false);
        ArrayList<ProfileItem> profileList = new ArrayList<>();

        fab = root.findViewById(R.id.fab);
        container.clearDisappearingChildren();

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddProfileFragment fragment = new AddProfileFragment();
                Bundle args = new Bundle();
                fragment.setArguments(args);
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.profile_container, fragment);
                transaction.commit();


            }
        });

我要替换的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ui.profile.ProfileFragment"
    android:backgroundTint="@color/white"
    android:id="@+id/profile_container" >

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

    <TextView
        android:id="@+id/text_profile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:fontFamily="@font/nunito_regular"
        android:text="@string/profiles"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.024"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.097"
        android:layout_below="@+id/up_toolbar"/>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:id="@+id/profileRecView"
        android:scrollbars="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_profile"
        android:layout_below="@+id/text_profile"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="end|bottom"
        android:layout_marginRight="29dp"
        android:layout_marginBottom="70dp"
        android:backgroundTint="@color/primary_green"
        android:contentDescription="Submit"
        android:src="@drawable/ic_add" />

</RelativeLayout>

我访问个人资料片段的家庭活动:

  public class HomeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        Toolbar toolbar = findViewById(R.id.nav_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_data, R.id.navigation_profile)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);


    }
}

重叠的添加配置文件的布局:

 <?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=".ui.addProfile.AddProfileFragment">


    <EditText
        android:id="@+id/editTextHumidityLow"
        android:layout_width="60sp"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintStart_toEndOf="@+id/textView14"
        app:layout_constraintTop_toBottomOf="@+id/textView13" />

    <EditText
        android:id="@+id/editTextHumidityHigh"
        android:layout_width="60sp"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintStart_toEndOf="@+id/textView12"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="25sp"
        android:layout_height="25sp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="38dp"
        android:src="@drawable/ic_thermostat"
        app:layout_constraintStart_toEndOf="@+id/textView13"
        app:layout_constraintTop_toBottomOf="@+id/editTextCO2High"
        app:srcCompat="@drawable/ic_carbon_humidity" />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="28dp"
        android:layout_marginTop="38dp"
        android:fontFamily="@font/nunito_bold"
        android:text="Humidity threshold"
        android:textColor="@color/black"
        android:textSize="17sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextCO2Low" />

    <TextView
        android:id="@+id/textView14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="28dp"
        android:layout_marginTop="32dp"
        android:fontFamily="@font/nunito_regular"
        android:text="Low:"
        android:textColor="@color/black"
        android:textSize="17sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView13" />

    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="60dp"
        android:layout_marginTop="28dp"
        android:fontFamily="@font/nunito_regular"
        android:text="High:"
        android:textColor="@color/black"
        android:textSize="17sp"
        app:layout_constraintStart_toEndOf="@+id/editTextHumidityLow"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

    <TextView
        android:id="@+id/textView10"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="38dp"
        android:fontFamily="@font/nunito_bold"
        android:text="CO2 threshold"
        android:textColor="@color/black"
        android:textSize="17sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTemperatureLow" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="28dp"
        android:text="Low:"
        android:fontFamily="@font/nunito_regular"
        android:textColor="@color/black"
        android:textSize="17sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView10" />

    <EditText
        android:id="@+id/editTextCO2Low"
        android:layout_width="60sp"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintStart_toEndOf="@+id/textView9"
        app:layout_constraintTop_toBottomOf="@+id/textView10" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="56dp"
        android:layout_marginTop="28dp"
        android:text="High:"
        android:fontFamily="@font/nunito_regular"
        android:textColor="@color/black"
        android:textSize="17sp"
        app:layout_constraintStart_toEndOf="@+id/editTextCO2Low"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />

    <EditText
        android:id="@+id/editTextCO2High"

        android:layout_width="60sp"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintStart_toEndOf="@+id/textView11"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="25sp"
        android:layout_height="25sp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="38dp"
        android:src="@drawable/ic_thermostat"
        app:layout_constraintStart_toEndOf="@+id/textView10"
        app:layout_constraintTop_toBottomOf="@+id/editTextTemperatureHigh"
        app:srcCompat="@drawable/ic_iwwa_co2" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="44dp"
        android:fontFamily="@font/nunito_bold"
        android:text="Create a new profile"
        android:textColor="@color/black"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="36dp"
        android:fontFamily="@font/nunito_bold"
        android:text="Name of the profile:"
        android:textColor="@color/black"
        android:textSize="17sp"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <EditText
        android:id="@+id/editTextProfileName"
        android:layout_width="170sp"
        android:layout_height="45sp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="30dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="34dp"
        android:layout_marginTop="38dp"
        android:fontFamily="@font/nunito_bold"
        android:text="Temperature threshold"
        android:textColor="@color/black"
        android:textSize="17sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="33dp"
        android:layout_marginTop="30dp"
        android:text="Low:"
        android:fontFamily="@font/nunito_regular"
        android:textColor="@color/black"
        android:textSize="17sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView5" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="60dp"
        android:layout_marginTop="30dp"
        android:text="High:"
        android:fontFamily="@font/nunito_regular"
        android:textColor="@color/black"
        android:textSize="17sp"
        app:layout_constraintStart_toEndOf="@+id/editTextTemperatureLow"
        app:layout_constraintTop_toBottomOf="@+id/textView5" />

    <EditText
        android:id="@+id/editTextTemperatureLow"
        android:layout_width="60sp"
        android:layout_height="wrap_content"
        android:layout_marginStart="3dp"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintStart_toEndOf="@+id/textView7"
        app:layout_constraintTop_toBottomOf="@+id/textView5" />

    <EditText
        android:id="@+id/editTextTemperatureHigh"
        android:layout_width="60sp"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintStart_toEndOf="@+id/textView8"
        app:layout_constraintTop_toBottomOf="@+id/textView5" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="25sp"
        android:layout_height="25sp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="30dp"
        android:src="@drawable/ic_thermostat"
        app:layout_constraintStart_toEndOf="@+id/textView5"
        app:layout_constraintTop_toBottomOf="@+id/editTextProfileName" />

    <Button
        android:id="@+id/button_save_profile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="40dp"
        android:backgroundTint="@color/primary_green"
        android:text="Save"
        android:fontFamily="@font/nunito_bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextHumidityHigh" />

    <Button
        android:id="@+id/button_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="16dp"
        android:text="Cancel"
        android:fontFamily="@font/nunito_bold"
        android:backgroundTint="@color/faded_gray"
        android:textColor="@color/black"
        app:layout_constraintEnd_toStartOf="@+id/button_save_profile"
        app:layout_constraintTop_toBottomOf="@+id/editTextHumidityHigh" />

</androidx.constraintlayout.widget.ConstraintLayout>

标签: javaandroidandroid-fragmentsandroid-architecture-componentsandroid-navigation

解决方案


AddProfileFragment fragment = new AddProfileFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.profile_container, fragment);
transaction.commit();

您正在尝试用 id 替换容器中的片段R.id.profile_container,实际上这个 id 不是用于活动布局中的容器,而是用于当前片段的根布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ui.profile.ProfileFragment"
    android:backgroundTint="@color/white"
    android:id="@+id/profile_container"
    >
....

要解决此问题,您需要在托管共享片段的活动中拥有一些容器/占位符 ViewGroup,并profile_container用该 ID 替换下面的脚本

transaction.replace(R.id.profile_container, fragment);

更新

第一个片段事务由 AppBarConfiguration 处理

现在您正在使用导航架构组件进行片段事务,这是通过第一个片段事务完成的,但是您使用FragmentTransaction了第二个片段,尽管navController导航架构组件中负责片段事务。

因此,要解决您需要使用navController第二笔交易:

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.fragment_profile, container, false);
    ArrayList<ProfileItem> profileList = new ArrayList<>();

    fab = root.findViewById(R.id.fab);
    container.clearDisappearingChildren();

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        
            Bundle args = new Bundle();
            
            NavHostFragment navHostFragment = (NavHostFragment) mActivity.getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment); 
            if (navHostFragment != null) {
                NavController navController = navHostFragment.getNavController();
                navController.navigate(R.id.navigation_profile, args);
            }
        }
    });

推荐阅读