Skip to content Skip to sidebar Skip to footer

Android Google Maps Dynamic Fragment In Recyclerview Holder

I'm trying to add Google Maps fragments into a RecyclerView in Android. I was reading about it and I saw that I need to create the fragments dynamically instead of doing it by XML

Solution 1:

I assume bindMapRow is called at onBindViewHolder.

The reason for the error IllegalArgumentException: No view found for id is that holder.mapContainer and frame is not attached to the RecyclerView/Activity yet during onBindViewHolder (thus the view not found error).

To guarantee the view is already attached to RecyclerView/Activity before calling FragmentTransaction.replace, listen to onViewAttachedToWindow instead.

classMyAdapterextendsRecyclerView.Adapter<MyAdapter.ViewHolder> {
    ...
    @OverridepublicvoidonViewAttachedToWindow(ViewHolder holder) {
        super.onViewAttachedToWindow(holder);

        // If you have multiple View Type, check for the correct viewType SupportMapFragmentmapFragment= holder.mapFragment;
        if (mapFragment == null) {
            mapFragment = SupportMapFragment.newInstance();
            mapFragment.getMapAsync(newOnMapReadyCallback() {
                @OverridepublicvoidonMapReady(GoogleMap googleMap) {
                    ...
                }
            });
        }

        FragmentManagerfragmentManager= getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.map, mapFragment).commit();
    }
}

https://code.luasoftware.com/tutorials/android/supportmapfragment-in-recyclerview/

Post a Comment for "Android Google Maps Dynamic Fragment In Recyclerview Holder"