airbnb / mavericks

Mavericks: Android on Autopilot
https://airbnb.io/mavericks/
Apache License 2.0
5.83k stars 500 forks source link

Google maps and BottomNavigationView #100

Closed jQrgen closed 6 years ago

jQrgen commented 6 years ago

Hello

I'm trying to setup google maps inside of BottomNavigationView tab using MvRx.

I have to questions:

  1. What is the best pracice for setting up BottomNavigationView in MvRx?
  2. Using the code below I managed to load the map once but the second time i open the tab i get the following error. Therefore i am wondering what the recommended way to setup google maps in a BottomNavigationView fragment using MvRx is?
Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f080089, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment

My fragment looks like this:

class MapFragment : BaseFragment() {

    private val mViewModel: ChatViewModel by fragmentViewModel()

    override fun epoxyController() = simpleController(mViewModel) {state ->
        mapView{
            id("map")
        }
    }
}

And view:

@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_MATCH_HEIGHT)
class MapView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    init {
        inflate(context, R.layout.fragment_map, this)
    }

}

Map fragment (Crashes on second inflate)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</FrameLayout>

main_activity

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@android:color/white"
            app:itemTextColor="@color/bottom_navigation_text"
            app:layout_behavior="android.support.design.behavior.HideBottomViewOnScrollBehavior"
            app:menu="@menu/bottom_navigation_main" />

</android.support.design.widget.CoordinatorLayout>

MainActivity

class MainActivity: BaseMvRxActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setupNavigation()
    }

    private fun setupNavigation() {
        val navController = findNavController(R.id.my_nav_host_fragment)
        bottom_navigation.setupWithNavController(navController)
    }

    override fun onSupportNavigateUp() =
            findNavController(R.id.my_nav_host_fragment).navigateUp()

}
jQrgen commented 6 years ago

Moved to stackoverflow where it belongs: https://stackoverflow.com/questions/52555009/google-maps-and-bottomnavigationview