Issue
I am trying to implement a RecyclerView containing CardViews with each one MapView inside. The problem is, that the MapView, initialized in the custom Adapter, does not load, unless you click on it. As already mentioned here: Android MapView does not load unless you touch on the mapview you can solve the problem by overriding the onResume(). I have tried this the following way:
@Override
public void onResume() {
if(LocationsAdapter.ViewHolder.mapView != null) {
LocationsAdapter.ViewHolder.mapView.onResume();
}
super.onResume();
}
Honestly I simply had no idea how to override the onResume in any other way, please forgive me if I broke certain rules of programming here. I made the mapView as public static inside the ViewHolder created in the LocationsAdapter. This does not seem to work, the map is still blank.
The ViewHolder inside the Adapter implements OnMapReadyCallback.
public static class ViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback
...
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
Location location = locations.get(getAdapterPosition());
this.googleMap.addMarker(new MarkerOptions().position(location.getLatlng()).title(location.getName()));
this.googleMap.moveCamera(CameraUpdateFactory.newLatLng(location.getLatlng()));
this.googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location.getLatlng(), 7.0f));
}
These two methods are called when the ViewHolder is called.
mapView.onCreate(null);
mapView.getMapAsync(this);
Another article referring to this problem is unanswered yet: mapView inside cardview not load the map.
My goal is, to load the map no matter what lifecycle the fragment is currently in. I assume you will neither need the Adapter, the Fragment nor the XML for this problem. If you do though, let me know please.
Solution
Don't use map view inside recycler view as it is heavy component it consumes more memory, instead use lite mode of maps which is specifically design for listing with recycler view/ list view, for more details refer this link https://developers.google.com/maps/documentation/android-sdk/lite
Also google has provided demo on how to use Lite mode map with recycler view, here's the link to GitHub https://github.com/googlemaps/android-samples/blob/master/ApiDemos/java/app/src/main/java/com/example/mapdemo/LiteListDemoActivity.java
Answered By - SSB
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.