我如何获取由NavHostFragment(导航架构组件)加载的Fragment(SupportMapFragment)的引用

狮子座300

我正在尝试在google IO 2018会议中介绍的导航架构组件。我正在尝试制作一个带有三个选项卡的应用程序,每个选项卡一个片段。在其中之一中,我想使用Google Maps API放置地图。通常,您应该在活动的OnCreate中执行此操作

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
        .findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

R.id.map isSupportMapFragment内部的IDmain_activity.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />

但是,导航体系结构组件的设置有些不同。main_activity.xml仅包含NavHostFragment(加载了每个屏幕片段的位置)和BottomNavigationView在应用程序的不同目标之间移动的。

<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<fragment
    android:id="@+id/my_nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/nav_graph"
    app:defaultNavHost="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent" >

</fragment>


<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@android:color/white"
    android:layout_alignParentBottom="true"
    app:itemTextColor="@android:color/white"
    app:menu="@menu/bottom_navigation_main" />

</RelativeLayout>

我的导航图如下所示

<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"
app:startDestination="@id/map">

<fragment
    android:id="@+id/endFragment"
    android:name="douglas.leone.easytaxi.EndFragment"
    android:label="fragment_end"
    tools:layout="@layout/fragment_end" />

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />

<fragment
    android:id="@+id/startFragment"
    android:name="douglas.leone.easytaxi.StartFragment"
    android:label="fragment_start"
    tools:layout="@layout/fragment_start" />

</navigation>

正如你可以看到有两个片段startFragment,并endFragment在第一和第三个选项卡。在第二个中SupportMapFragment,我正在尝试引用。我无法使用标准getSupportFragmentManager().findFragmentById(int id)方式,因为main_activity.xml仅包含一个NavHostFragment

我尝试进行实验getChildFragmentManager()没有太大的成功。我也尝试过,navController.getCurrentDestination()但是它返回了一个NavDestination我无法用来检索已加载片段的对象。

我发现的唯一解决方案是SupportMapFragment直接添加activity_main.xml使它与导航图完全分离,但这比解决方案更具解决方法,因为当用户在另一个屏幕中时,我必须手动处理显示和隐藏地图。

如果有人知道适当的解决方案,请分享。

伊恩汉尼巴拉克

而不是SupportMapFragment直接将其添加到导航图,您应该添加自己的Fragment类,该类将拥有并管理SupportMapFragment-它将被调用getMapAsync并具有SupportMapFragment其自己的布局。

导航的目的是使活动与各个目的地的内容脱钩。不建议让活动管理单个目标。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何导航到Half Fragment?(导航架构组件)

如何在使用导航组件加载 Fragment 后杀死它

如何使用导航架构组件创建BottomSheetDialogFragment?

调用Fragment构造函数导致异常。导航架构组件

如何在不重新加载导航组件架构的情况下导航到上一个片段

如何在更改全屏/上方导航时使用导航组件navhostfragment

如何获取NavHostFragment

导航架构组件-活动

Android导航架构组件-获取当前可见的片段

如何使用导航架构组件在应用栏中设置标题

当我需要将引用传递给另一个组件时,如何确保加载引用?

如何从子组件获取对Blazor MainLayout的引用

如何获取我使用自定义闪电组件的所有引用页面?

如何在我的本机Redux应用程序中从组件方法获取对调度方法的引用

单击时如何在导航栏中引用组件

我如何让它引用 React 组件?

Mongodb:如何通过我的API填充我的架构中引用的架构

angular2路由器导航无法正确加载组件

Vue.js 2路由器仅从导航而不是从URL加载组件

如何从Mobiscroll子组件获取引用到父组件

带有导航架构组件的ViewPager

具有架构组件的多模块导航

导航架构组件 - 登录按钮 - createNavigateOnClickListener

关于android导航架构组件的问题

我有一个由itemid定义的组件,该如何在控制器中获取它

如何从 ViewPager2 的 NavHostFragment 内的片段导航回来?

如何获取此组件以加载“编辑页面”组件

如何使用 Android Jetpack 的导航组件禁用后退导航并删除 Fragment 上的后退箭头?

如何在导航栏中获取我的导航链接?