Android Google Maps dynamic fragment in RecyclerVi

2019-06-27 07:28发布

问题:

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 to avoid the duplicate ID errors.

Here is my adapter code:

public class MapViewHolder extends RecyclerView.ViewHolder {
    protected FrameLayout mapContainer;

    public MapViewHolder(View v){
        super(v);
        view = v;
        mapContainer = (FrameLayout) v.findViewById(R.id.mapContainer);
    }
}

private void bindMapRow(final MapViewHolder holder, int position) {
        MessageData message = (MessageData) messagesList.get(position);
        LocationData location = message.getLocation();

        FrameLayout view = holder.mapContainer;
        FrameLayout frame = new FrameLayout(context);
        if (message.getIdMap() != 0) {
            frame.setId(message.getIdMap());
        }
        else {
            message.setIdMap(generateViewId());
            frame.setId(message.getIdMap());
            messagesList.set(position, message);
        }

        int size = context.getResources().getDimensionPixelSize(R.dimen.row_chat_map_size);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(size, size);
        frame.setLayoutParams(layoutParams);

        view.addView(frame);

        GoogleMapOptions options = new GoogleMapOptions();
        options.liteMode(true);

        SupportMapFragment mapFrag = SupportMapFragment.newInstance(options);
        mapFrag.getMapAsync(new MapReadyCallback(location));

        FragmentManager fm = activity.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(frame.getId(), mapFrag);
        ft.commit();
}

Executing this I can see the map displayed into my RecyclerView and all seems to be working well. The problem appears when I scroll up in the RecyclerView for a while and then I go back again to the map.

When I do that the app crashes and shows this error:

java.lang.IllegalArgumentException: No view found for id 0x1 (unknown) for fragment SupportMapFragment{606864b #0 id=0x1}

Many thanks and kind regards, Marcel.

回答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.

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    ...
    @Override
    public void onViewAttachedToWindow(ViewHolder holder) {
        super.onViewAttachedToWindow(holder);

        // If you have multiple View Type, check for the correct viewType 
        SupportMapFragment mapFragment = holder.mapFragment;
        if (mapFragment == null) {
            mapFragment = SupportMapFragment.newInstance();
            mapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    ...
                }
            });
        }

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

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