0

나는 RcyclerView 어댑터의 내 구현 구글 라이트 mode.here지도를 사용하여 각 항목의 위치를 ​​표시하려고하는 recyclerView 있습니다구글지도 라이트 모드는 목록 업데이트 후 실행 OnMapReady

public class ItemListAdapter extends RecyclerView.Adapter<ItemListAdapter.MyViewHolder>{ 

    List<ItemListModel> items = new ArrayList<>(); 
    Context context; 

    public ItemListAdapter(Context context) { 
     this.context = context; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.cardview_item,parent, false); 
     return new MyViewHolder(v,context); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
      ItemListModel item = items.get(position); 
      holder.setMapLocation(new LatLng(item.getLat(), item.getLon())); 
    } 

    @Override 
    public int getItemCount() { 
     return items.size(); 
    } 


    @Override 
    public void onViewRecycled(MyViewHolder holder) 
    { 
     // Cleanup MapView here? 
     if (holder.gMap != null) 
     { 
      holder.gMap.clear(); 
      holder.gMap.setMapType(GoogleMap.MAP_TYPE_NONE); 
     } 
    } 
    public void setItmes(List<ItemListModel> items){ 
     this.items.addAll(items); 
     notifyDataSetChanged(); 
    } 

    public class MyViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback{ 

     GoogleMap gMap; 
     MapView map; 
     LatLng mMapLocation; 
     Context context; 

     public MyViewHolder(View itemView,Context context) { 
      super(itemView); 

      map = (MapView) itemView.findViewById(R.id.mapImageView); 
      this.context = context; 
      if (map != null) 
      { 
       map.onCreate(null); 
       map.onResume(); 
       map.getMapAsync(this); 
      } 
     } 

     @Override 
     public void onMapReady(GoogleMap googleMap) { 

      MapsInitializer.initialize(context); 
      gMap = googleMap; 
     } 
     public void setMapLocation(LatLng mapLocation) { 
      mMapLocation = mapLocation; 

      if(gMap != null) { 
       updateMapContents(); 
      } 
     } 
     protected void updateMapContents() { 
      gMap.clear(); 
      gMap.addMarker(new MarkerOptions().position(mMapLocation)); 
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(mMapLocation, 15f); 
      gMap.moveCamera(cameraUpdate); 
     } 
    } 

} 

문제를 myViewHolder에 내 목록을 발행 한 후 onMapReady,에 onMapReady 항상 실행하고 있기 때문에 GMAP 목록 업데이트 중 null 인 그. setMapLocation이 예상대로 작동하지 않습니다. Recyclerview Item의 MapView는 항상 비어 있습니다.

어디에서 코드를 잘못하고 있습니까? 나는이 문제를 해결하기 위해 무슨 짓을

+0

지도를 준비한 후 어댑터를 만든 다음 recyclerview에 바인딩합니다. –

답변

0

이었다

onBindViewHolder에서
  1. 는(), 표시하려는 LatLng를 할 수 있도록 ViewHolder의 maptag을 설정합니다. (즉, map.tag = new LatLng(item.getLat(), item.getLon())). ViewHolder에서 다른 멤버 변수를 사용하여 이미 수행 한 LatLng 값을 일시적으로 보유하도록 선택할 수 있습니다.

  2. ViewHolder 인스턴스가 처음으로 onCreateViewHolder()를 통해 만들어지면지도가 초기화되고 onMapReady()는 AFTER onBindViewHolder()라고합니다. 이제 onMapReady()에서 map.tag에 방금 설정 한 LatLng 또는 이전에 설정 한 변수를 사용합니다. 귀하의 경우 전화로 if (gMap != null) updateMapContents()

이렇게하면 문제가 해결됩니다. 이 솔루션에서 영감을 받았습니다. https://github.com/androidcodegeeks/android-map_list/tree/master/app/src/main/java/com/example/google/maplist

동일한 문제가있어 도움이되는지 알려주세요.