Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式将片段加载到导航中_Android_Android Fragments_Android Navigation - Fatal编程技术网

Android 以编程方式将片段加载到导航中

Android 以编程方式将片段加载到导航中,android,android-fragments,android-navigation,Android,Android Fragments,Android Navigation,我在androidstudio中从导航抽屉活动模板创建了一个新项目,我有以下xml content_main.xml <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:

我在androidstudio中从导航抽屉活动模板创建了一个新项目,我有以下xml

content_main.xml

<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

据我所知,原因是product_list片段不是以编程方式添加的,因此不能被FragmentManager替换。如果是这种情况,如何以编程方式加载该片段?我读了数百篇文章,但我仍然感到困惑,因为在我的案例中,涉及到导航问题。

我为您提供了一个黑客解决方案:


在布局中添加容器,根据需要隐藏和取消隐藏片段和容器

我解决了这个问题,摆脱了
FragmentTransaction
s,将所有片段定义为导航目的地。本教程非常有用:


使用导航时,您不应该执行任何
碎片事务。这本书讲述了你应该做的事情。
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/mobile_navigation"
    app:startDestination="@+id/nav_shopping_list">

    <fragment
        android:id="@+id/nav_products_list"
        android:name="com.example.producttracker.ui.products_list.ProductsListFragment"
        android:label="@string/menu_products_list"
        tools:layout="@layout/fragment_products_list" />

</navigation>
                AppCompatActivity activity = (AppCompatActivity) view.getContext();

                Fragment inputFragment = new ProductInputFragment();


                FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.nav_host_fragment, inputFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();