我正在尝试在google IO 2018会议中介绍的导航架构组件。我正在尝试制作一个带有三个选项卡的应用程序,每个选项卡一个片段。在其中之一中,我想使用Google Maps API放置地图。通常,您应该在活动的OnCreate中执行此操作
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
R.id.map is
SupportMapFragment
内部的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] 删除。
我来说两句