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

时间:2019-10-11 18:44:32

标签: android android-fragments android-navigation

我从android studio中的“导航抽屉活动模板”创建了一个新项目,并且具有以下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>

mobile_navigation.xml

<?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>

问题是,当我在product_list片段中有一个按钮时,单击该按钮应加载一个新片段,以替换当前片段。但是,目前,它只是在现有片段之上加载了一个新片段。

用于加载新片段的代码:

                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();

据我了解,原因是product_list片段不是以编程方式添加的,因此无法由FragmentManager替换。如果是这种情况,我如何以编程方式加载该片段?我读了数百篇文章,但仍然感到困惑,因为就我而言,涉及导航。

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我解决了这个问题,摆脱了SELECT U.user_name, LISTAGG(B.title || 'at' || B.date, ',') WITHIN GROUP (ORDER BY B.date) FROM users U JOIN book B ON U.book_id = B.id GROUP BY U.user_name 并将所有片段都定义为导航的目的地。本教程非常有用:

https://developer.android.com/guide/navigation/navigation-getting-started

答案 2 :(得分:0)

Simply use Fragment manager to change the fragment.
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = 
getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

//   Commit the transaction
transaction.commit();