I am trying to add fragments of Google Maps to RecyclerView on Android.
I read about it, and I saw that I needed to create fragments dynamically, and not do it using XML to avoid 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();
}
By doing this, I see the map displayed in my RecyclerView, and everything seems to work fine. The problem arises when I scroll for a while in RecyclerView and then return to the map again.
When I do this, the application crashes and shows this error:
java.lang.IllegalArgumentException: for id 0x1 no views were found (unknown) for the fragment SupportMapFragment {606864b # 0 id = 0x1}
Thanks a lot and kind, Marcel.