I have used map view inside recyclerview row. I have attached screenshot. While scrolling maps display black flickering background.
Adapter code:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TASK_VIEW) {
return new TasksViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_task_row, parent, false));
} else if (viewType == CHECKIN_VIEW) {
return new CheckInViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_checkin_row, parent, false));
} else {
return null;
}
}
@Override
private void onBindCheckInViewHolder(CheckInViewHolder holder, int position) {
final JobCheckIn singleCheckIn = (JobCheckIn) items.get(position);
holder.location.setText(singleCheckIn.getAddress());
holder.lastUpdated.setText(String.valueOf("Created " +
activity.getDateTime(singleCheckIn.getCreatedDateTime())));
holder.deleteTask.setOnClickListener(view -> jobInterface.deleteCheckIn(singleCheckIn.getId()));
float f = 0.0f;
if (holder.mapView != null && Float.compare(singleCheckIn.getLatitude(), f) != 0 &&
Float.compare(singleCheckIn.getLongitude(), f) != 0) {
holder.mapView.setVisibility(View.VISIBLE);
holder.initializeMapView();
if (holder.gMap != null) {
// The map is already ready to be used
moveMap(holder.gMap, singleCheckIn.getLatitude(), singleCheckIn.getLongitude());
}
}
}
public class CheckInViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
MapView mapView;
GoogleMap gMap;
public CheckInViewHolder(View view) {
super(view);
mapView = view.findViewById(R.id.checkInMapView);
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(context);
gMap = googleMap;
googleMap.getUiSettings().setMapToolbarEnabled(false);
if(items.get(getLayoutPosition())!=null )
moveMap(gMap,((JobCheckIn) items.get(getLayoutPosition())).getLatitude(), ((JobCheckIn) items.get(getLayoutPosition())).getLongitude());
}
public void initializeMapView() {
if (mapView != null) {
// Initialise the MapView
mapView.onCreate(null);
// Set the map ready callback to receive the GoogleMap object
mapView.getMapAsync(this);
}
}
}
public void moveMap(GoogleMap gMap, double latitude, double longitude) {
Log.v(TAG, "mapMoved: " + gMap);
LatLng latlng = new LatLng(latitude, longitude);
CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(latlng, 6);
gMap.addMarker(new MarkerOptions().position(latlng));
gMap.moveCamera(cu);
}
I refer this link: Link but in this demo they used array adapter in my code we have used recyclerview adapter.
xml code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.google.android.gms.maps.MapView
android:id="@+id/checkInMapView"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:enabled="true"
android:visibility="gone"
map:cameraZoom="15"
map:liteMode="true"
map:mapType="normal" />
<!-- I have also tried adding transparent view as below but still no solution.
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"/>-->
</LinearLayout>
I am not performing any zoom-In, Zoom-out or drag. Thanks in advance.
Google Issue Track
Update
Previously i was initializing map in viewHolder instead of mentioned as above scenario.
onBindCheckInViewHolder(...){
holder.mapView.onCreate(null);
holder.mapView.getMapAsync(googleMap -> {
MapsInitializer.initialize(context);
gMap = googleMap;
googleMap.getUiSettings().setMapToolbarEnabled(false);
moveMap(gMap, singleCheckIn.getLatitude(), singleCheckIn.getLongitude());
});
}
Write this line in AndroidManifest.xml:
android:hardwareAccelerated="true"
<application
android:name="com.xyz"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
</application>
It works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With