我有一个使用一个Android Studio模板创建的MapsActivity,并且在底部添加了一个导航栏,其中有3个图标(“地图活动”,“片段A”和“片段B”)。我可以显示片段并在它们之间切换,也可以切换回MapsActivity
。
问题:当我单击“地图”活动图标时,要从某个片段返回到MapsActivity
,该片段将分离并且我会看到我的{{ 1}},但Google Maps片段不会被“冻结”,我无法滚动。但是我的搜索栏顶部仍然是另一个静态片段。
这是我的实现方式:
MapsActivity.kt
MapsActivity
activity_maps.xml
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
val bottomNavigation: BottomNavigationView = findViewById(R.id.bottom_navigation)
bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
getAutoCompleteSearchResults()
}
...
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener {item->
when(item.itemId){
R.id.nav_map -> {
Log.d(TAG, "map pressed")
// if there's a fragment, detach it
val visibleFrag = getVisibleFragment()
if(visibleFrag != null){
detachFragment(visibleFrag)
}
return@OnNavigationItemSelectedListener true
}
R.id.nav_A -> {
Log.d(TAG, "Fragment A pressed")
replaceFragment(FragmentA())
return@OnNavigationItemSelectedListener true
}
R.id.nav_B -> {
Log.d(TAG, "Fragment B pressed")
replaceFragment(FragmentB())
return@OnNavigationItemSelectedListener true
}
}
false
}
...
private fun replaceFragment(fragment: Fragment){
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentContainer, fragment)
fragmentTransaction.commit()
}
...
private fun getVisibleFragment(): Fragment? {
val fragmentManager= this@MapsActivity.supportFragmentManager
val fragments = fragmentManager.fragments
if (fragments != null) {
for (fragment in fragments!!) {
if (fragment != null && fragment!!.isVisible())
return fragment
}
}
return null
}
private fun detachFragment(fragment: Fragment){
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.remove(fragment)
fragmentTransaction.commit()
}
编辑
我想我知道为什么每次我拆离碎片时地图碎片都会“凝结”。这是因为在我的<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fragmentContainer">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@id/place_autocomplete_fragment"
android:layout_above="@id/bottom_navigation"
/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
app:menu="@menu/bottom_nav_menu"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
app:itemIconTint="@color/common_google_signin_btn_text_dark_default"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_marginBottom="0dp"
android:layout_alignParentLeft="true" android:layout_marginLeft="0dp"
android:layout_alignParentStart="true" android:layout_marginStart="0dp"/>
</RelativeLayout>
函数中,我遍历了包括我的getVisibleFragment
在内的所有片段。如果片段为supportMapFragment
编辑1.1
我曾尝试在supportMapFragment?
下进行此操作,但仍无法解决。
R.id.nav_map -> {