Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.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 Jetpack_Android Jetpack Navigation - Fatal编程技术网

Android无限片段导航

Android无限片段导航,android,android-jetpack,android-jetpack-navigation,Android,Android Jetpack,Android Jetpack Navigation,必须在导航图中注册目标片段吗?例如,如果我想通过片段显示多级目录,并且每个片段显示一个目录,那么我无法在导航视图中注册所有片段,因为我不知道需要多少片段。 我该怎么办 -------加-------- 也许我没有清楚地描述我的问题,我目前的需求如下: 现在我使用ViewPager和FragmentStatePagerAdapter动态添加和删除片段,但我想迁移到导航,我不知道是否有办法。在导航图中,您应该只有一个目录片段。该片段将具有一个指示应显示的目录: <fragment andro

必须在导航图中注册目标片段吗?例如,如果我想通过片段显示多级目录,并且每个片段显示一个目录,那么我无法在导航视图中注册所有片段,因为我不知道需要多少片段。
我该怎么办

-------加--------

也许我没有清楚地描述我的问题,我目前的需求如下:


现在我使用ViewPager和FragmentStatePagerAdapter动态添加和删除片段,但我想迁移到导航,我不知道是否有办法。

在导航图中,您应该只有一个
目录片段。该片段将具有一个指示应显示的目录:

<fragment android:id="@+id/directory_fragment"
    android:name="com.example.DirectoryFragment">
    <argument
        android:name="directory"
        app:argType="string"
        android:defaultValue="/" />
</fragment>
通过调用
navigate()
,将创建
DirectoryFragment
的新实例,并将其添加到后堆栈中,显示作为参数传入的目录

这是你所需要的最低限度。强烈建议您,它允许您并允许使用将类型安全性添加到
navigate()
调用中

这将允许您编写如下内容:

<fragment android:id="@+id/directory_fragment"
    android:name="com.example.DirectoryFragment">
    <argument
        android:name="directory"
        app:argType="string"
        android:defaultValue="/" />
    <action android:id="@+id/show_subdirectory"
        app:destination="@+id/directory_fragment"
        app:enterAnim="@anim/slide_in_right"
        app:exitAnim="@anim/slide_out_left"
        app:popEnterAnim="@anim/slide_in_left"
        app:popExitAnim="@anim/slide_out_right"/>
</fragment>

应该自行管理FragmentManager和FragmentTransaction,而不是使用导航组件现在我使用ViewPager和FragmentStatePagerAdapter动态添加和删除片段,这可能更简单。感谢您的回复!也许我没有清楚地描述我的问题。我修改了我的问题,上传的图片可能有用而且清晰。是的,这是一个完全不同的问题。我修改了我的答案以符合你的实际问题。
<fragment android:id="@+id/directory_fragment"
    android:name="com.example.DirectoryFragment">
    <argument
        android:name="directory"
        app:argType="string"
        android:defaultValue="/" />
    <action android:id="@+id/show_subdirectory"
        app:destination="@+id/directory_fragment"
        app:enterAnim="@anim/slide_in_right"
        app:exitAnim="@anim/slide_out_left"
        app:popEnterAnim="@anim/slide_in_left"
        app:popExitAnim="@anim/slide_out_right"/>
</fragment>
File directory = ...
directoryButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Navigation.findNavController(view).navigate(
            DirectoryFragmentDirections.showSubdirectory()
                .setDirectory(directory.getAbsolutePath()));
    }
});