android - 将 Google map 添加到 RecyclerView

标签 android android-mapview android-maps-v2

我已经在 RecyclerView 中添加了 map View 以及其他类型的列表项,但是现在......我如何以及在哪里初始化 map ,我在哪里监听 onMapReady 以便我可以放置之后的标记,我该如何处理元素的回收?

知道在这种情况下最佳做法是什么吗?

最佳答案

有两种可能的方式来做这件事,
一个是 Google Static Maps API使用,这将为您提供 map 的快照。

另一个是,您可以在回收器项目中使用 com.google.android.gms.maps.MapView 并在您的 viewholder 中初始化,如下所示,

public class AdapterWithMap extends RecyclerView.Adapter<AdapterWithMap.CustomeHolder> {

        @Override
        public void onBindViewHolder(CustomeHolder holder, int position)
        {
            GoogleMap thisMap = holder.mapCurrent;
            if(thisMap != null)
                thisMap.moveCamera();//initialize your position with lat long  or move camera
        }
        @Override
        public void onViewRecycled(CustomeHolder holder)
        {
            // Cleanup MapView here?
            if (holder.mapCurrent != null)
            {
                holder.mapCurrent.clear();
                holder.mapCurrent.setMapType(GoogleMap.MAP_TYPE_NONE);
            }
        }
        public class CustomeHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
            GoogleMap mapCurrent;
            MapView map;

            public CustomeHolder(View view) {
                super(view);
                map = (MapView) view.findViewById(R.id.mapImageView);
                if (map != null)
                {
                    map.onCreate(null);
                    map.onResume();
                    map.getMapAsync(this);
                }

            }

            @Override
            public void onMapReady(GoogleMap googleMap) {
                MapsInitializer.initialize(getApplicationContext());
                mapCurrent = googleMap;
            }

        }
    }

关于android - 将 Google map 添加到 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51209686/

相关文章:

Android Java - Joda Date 很慢

android - 将目标 SDK 更新到 android P 后 APK 安装失败

Android MapView + Tab 示例

Android计算多边形的面积

java - Google Maps v2 @ Android fragment 的 StrictMode 错误

android - Div 在 Android 上居中对齐但在 iPhone 上不居中

Android:android.view.InflateException:二进制 XML 文件第 13 行:三星 Galaxy S 中的错误膨胀类 <unknown>

android - 将 MapFragment 与 min sdk-8 一起使用

Android,如何从 XML 布局添加 Google map 选项?

Android - CircleView 内的 Google map