MapView在Recyclerview中无法正常工作

时间:2017-03-17 21:57:40

标签: android android-recyclerview android-mapview

有人可以请一些建议我如何在精简模式下使用Google Mapview进行Recyclerview工作?我已经建立了一个Recyclerview,我想要显示多个Mapview。当我使用recyclerview打开片段时,在recyclerview中可见区域之外的mapview不会更新他们的cameraMovement。再次打开片段后,一切都很好,所有Mapviews都会更新。如果我关闭应用程序然后再次启动它,那么可见区域之外的地图视图就不会再次更新...

正如您在AdapterCode中看到的那样,我在moveMap()方法中设置了一个由onMapReadyCallback()调用的Log输出。如果我滚动通过recyclerview我可以看到这个方法被正确调用,但奇怪的是,recyclerview最初可见区域之外的地图不会让他们的相机更新。有人可以告诉我我做错了吗?

这就是我在片段中设置recyclerview的方法:

mAdapter = new FragmentGPSRoutesAdapter(getContext(), routeList, waypointList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
recyclerView.setNestedScrollingEnabled(false);

下面你可以看到实际的recyclerview适配器:

public class FragmentGPSRoutesAdapter extends
    RecyclerView.Adapter<FragmentGPSRoutesAdapter.ViewHolder> {

    private List<Route> routeList;
    private List<Waypoint> waypointList;
    private Map<Long, ArrayList<Waypoint>> maps;
    private Context mContext;

    private static final int VIEW_TYPE_ROUTE = 1;
    private static final int VIEW_TYPE_EMPTY = 2;

    public class ViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback{
        public TextView tvRouteName, tvFrom, tvTo;

        public GoogleMap gMap;
        public MapView map;

        public ViewHolder(View view) {
            super(view);

            map = (MapView) view.findViewById(R.id.ivMap);
            if (map != null)
            {
                map.onCreate(null);
                map.getMapAsync(this);
            }

            tvRouteName = (TextView) view.findViewById(R.id.tvRouteName);
            tvFrom = (TextView) view.findViewById(R.id.tvFrom);
            tvTo = (TextView) view.findViewById(R.id.tvTo);
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            MapsInitializer.initialize(mContext);
            gMap = googleMap;
            gMap.getUiSettings().setMapToolbarEnabled(false);

            moveMap(gMap);
        }
    }

    public void moveMap(GoogleMap gMap) {
        Log.v("LOG", "mapMoved: " + gMap);
        LatLng latlng = new LatLng(52.3905217, 9.6996769);
        CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(latlng, 14);
        gMap.moveCamera(cu);
    }

    public FragmentGPSRoutesAdapter(Context mContext, List<Route> routeList, List<Waypoint> waypointList) {
        this.mContext = mContext;
        this.routeList = routeList;
        this.waypointList = waypointList;

        createWaypointHashmap();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        if (viewType == VIEW_TYPE_ROUTE) {
            itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_gps_route_list_row, parent, false);
        } else {
            itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_gps_empty, parent, false);
        }

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        int viewType = getItemViewType(position);
        if (viewType == VIEW_TYPE_ROUTE) {
            final Route route = routeList.get(position);

            // Show Routename with date
            holder.tvRouteName.setText(route.getName() + " - " + FormatTimestamp.showDate(mContext, route.getStartTime()));

            // Show Route Start/Destination Address
            holder.tvFrom.setText(route.getStartAddress());
            holder.tvTo.setText(route.getDestinationAddress());

        } else if (viewType == VIEW_TYPE_EMPTY) {
            // Do nothing here...
        }
    }

    @Override
    public int getItemCount() {
        if (routeList.size() == 0) {
            return 1;
        } else {
            return routeList.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (routeList.size() == 0)
            return VIEW_TYPE_EMPTY;
        else
            return VIEW_TYPE_ROUTE;
    }
    ...
}

1 个答案:

答案 0 :(得分:0)

您需要致电:

map.onResume();

在你之后:

map.onCreate(null);

让您的视图正确更新