MapView内部自定义RecyclerViewAdapter

Pro*_*roz -1 android android-mapview google-maps-android-api-2 android-recyclerview

我正在尝试使用每个MapView内部实现包含CardViews的RecyclerView.问题是,在自定义适配器中初始化的MapView不会加载,除非您单击它.正如这里已经提到的:Android MapView不会加载,除非您触摸mapview, 您可以通过覆盖onResume()来解决问题.我试过以下方式:

@Override
    public void onResume() {
        if(LocationsAdapter.ViewHolder.mapView != null) {
            LocationsAdapter.ViewHolder.mapView.onResume();
        }

        super.onResume();
    }
Run Code Online (Sandbox Code Playgroud)

老实说,我根本不知道如何以任何其他方式覆盖onResume,如果我在这里打破了某些编程规则,请原谅我.我将mapView作为在LocationsAdapter中创建的ViewHolder中的公共静态.这似乎不起作用,地图仍然是空白.

Adapter内的ViewHolder实现了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));
            }
Run Code Online (Sandbox Code Playgroud)

调用ViewHolder时会调用这两个方法.

mapView.onCreate(null);
mapView.getMapAsync(this);
Run Code Online (Sandbox Code Playgroud)

另一篇提到这个问题的文章还没有答案:cardview里面的mapView没有加载地图.

我的目标是,无论片段当前处于什么生命周期,都要加载映射.我假设您不需要适配器,片段和XML来解决此问题.如果你这样做,请告诉我.

SSB*_*SSB 8

不要在回收器视图中使用地图视图,因为它是重型组件,它消耗更多内存,而是使用精简模式的地图,专门设计用于列出回收站视图/列表视图,有关详细信息,请参阅此链接https:// developers. google.com/maps/documentation/android-sdk/lite

此外,谷歌提供了如何使用精简版模式地图与回收者视图的演示,这里是GitHub的链接https://github.com/googlemaps/android-samples/blob/master/ApiDemos/java/app/src/main/java /com/example/mapdemo/LiteListDemoActivity.java


小智 6

一定要在holder中调用onResume

if (mapView != null) {
                // Initialise the MapView
                mapView.onCreate(null);
                mapView.onResume();  //Probably U r missing this
                // Set the map ready callback to receive the GoogleMap object
                mapView.getMapAsync(this);
            }
Run Code Online (Sandbox Code Playgroud)