2017-05-09 76 views
1

這必須是有史以來最荒謬的事情。RecyclerView內的MapView僅在點擊上顯示地圖

我有重複的自定義項的RecyclerView。在這些項目中,有幾個文本框,按鈕和一個MapView

的問題是,當列表負載的MapView只顯示谷歌的標誌,沒有其他瓷磚或細節(或標記)。但是,當我在地圖上點擊一次時,它會顯示我添加的標記。在下一次點擊時,它會加載一個像素化的地圖。在另一個水龍頭上,它會加載更好質量的地圖。在進一步點擊時,它會爲附近的位置添加文本標籤。 LatLngBounds也不起作用,但這是次要問題。

這是怎麼發生的?

我的代碼如下:

JobAdapter.java

public class JobAdapter extends RecyclerView.Adapter<JobAdapter.ViewHolder> 
{ 
    private Context context; 
    private static List<Job> jobList; 
    private HashSet<MapView> mapViews = new HashSet<>(); 
    private GoogleMap googleMap; 

    public JobAdapter(Context con, List<Job> jobs) 
    { 
     context = con; 
     jobList = jobs; 
    } 

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

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    { 
     Context context = parent.getContext(); 
     LayoutInflater inflater = LayoutInflater.from(context); 
     View view = inflater.inflate(R.layout.listitem_booking, parent, false); 
     ViewHolder viewHolder = new ViewHolder(view); 
     mapViews.add(viewHolder.mapView); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) 
    { 
     Job job = jobList.get(position); 

     holder.mapView.setClickable(false); 

     if(job.getJobType().equalsIgnoreCase("Now")) 
     { 
      holder.pickup.setBackgroundColor(ContextCompat.getColor(context, R.color.lightRed)); 
     } 

     holder.pickup.setText(job.getPickupAddress()); 
     if(job.getDestinationAddress() != null && !job.getDestinationAddress().equalsIgnoreCase("")) 
     { 
      holder.destination.setText(job.getDestinationAddress()); 
     } 
     else 
     { 
      holder.destination.setVisibility(View.GONE); 
     } 
     holder.person.setText(job.getContact()); 
     holder.datetime.setText(job.getDate() + " at " + job.getTime()); 
    } 

    class ViewHolder extends RecyclerView.ViewHolder implements /*View.OnClickListener,*/ OnMapReadyCallback 
    { 
     @BindView(R.id.pickup) 
     TextView pickup; 

     @BindView(R.id.destination) 
     TextView destination; 

     @BindView(R.id.person) 
     TextView person; 

     @BindView(R.id.datetime) 
     TextView datetime; 

     @BindView(R.id.map_listitem) 
     MapView mapView; 

     @BindView(R.id.acceptJob) 
     Button acceptJob; 

     @BindView(R.id.declineJob) 
     Button declineJob; 

     @BindView(R.id.buttonLayout) 
     LinearLayout buttonLayout; 

     private ViewHolder(View itemView) 
     { 
      super(itemView); 
      ButterKnife.bind(this, itemView); 
//   itemView.setOnClickListener(this); 
      mapView.onCreate(null); 
      mapView.getMapAsync(this); 
     } 

     private void addMarkers(Job job) 
     { 
      googleMap.clear(); 
      boolean hasDestination = true; 
      String[] destinationLatlng = null; 
      LatLng destination = null; 

      if(job.getDestinationAddress() == null || job.getDestinationAddress().equalsIgnoreCase("")) 
      { 
       hasDestination = false; 
      } 
      else 
      { 
       destinationLatlng = job.getDestinationLatLong().split(","); 
       destination = new LatLng(Double.valueOf(destinationLatlng[0]), Double.parseDouble(destinationLatlng[1])); 
      } 

      final String[] pickupLatlng = job.getPickupLatLong().split(","); 
      final LatLng pickup = new LatLng(Double.valueOf(pickupLatlng[0]), Double.parseDouble(pickupLatlng[1])); 

      if(hasDestination) 
      { 
       googleMap.addMarker(new MarkerOptions() 
         .position(pickup) 
         .title(job.getPickupAddress())); 

       googleMap.addMarker(new MarkerOptions() 
         .position(destination) 
         .title(job.getDestinationAddress())); 

       LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
       builder.include(pickup); 
       builder.include(destination); 

       LatLngBounds bounds = builder.build(); 

       CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 5); 
       googleMap.animateCamera(cameraUpdate); 
      } 
      else 
      { 
       googleMap.addMarker(new MarkerOptions() 
         .position(pickup) 
         .title(job.getPickupAddress())); 

       CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(pickup, 15); 
       googleMap.animateCamera(cameraUpdate); 
      } 
     } 

     /*@Override 
     public void onClick(View view) 
     { 
      final Job job = jobList.get(getAdapterPosition()); 
     }*/ 

     @Override 
     public void onMapReady(GoogleMap gMap) 
     { 
      googleMap = gMap; 
      addMarkers(jobList.get(getAdapterPosition())); 
     } 
    } 
} 

listitem_booking

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:map="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    app:cardElevation="2dp" 
    android:layout_marginBottom="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginEnd="8dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:id="@+id/pickup" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingStart="4dp" 
      android:paddingEnd="4dp" 
      android:paddingTop="2dp" 
      android:paddingBottom="2dp" 
      android:textSize="16sp" 
      android:text="Complex" 
      android:background="@color/lessLightGreen" 
      android:gravity="center_vertical" 
      android:drawableStart="@drawable/google_maps" 
      android:drawablePadding="2dp" 
      android:textColor="@color/colorPrimaryText"/> 

     <TextView 
      android:id="@+id/destination" 
      android:layout_below="@id/pickup" 
      android:text="Golra" 
      android:visibility="visible" 
      android:drawableStart="@drawable/directions" 
      android:drawablePadding="2dp" 
      style="@style/listitem_secondary_text"/> 

     <TextView 
      android:id="@+id/person" 
      android:drawablePadding="2dp" 
      android:layout_below="@id/destination" 
      android:text="Asfandyar Khan" 
      android:drawableStart="@drawable/account" 
      style="@style/listitem_secondary_text"/> 

     <TextView 
      android:id="@+id/datetime" 
      android:layout_below="@id/person" 
      android:text="7th April 2017 at 9:00am" 
      android:drawableStart="@drawable/time" 
      style="@style/listitem_secondary_text"/> 

     <com.google.android.gms.maps.MapView 
      android:id="@+id/map_listitem" 
      android:layout_width="match_parent" 
      android:layout_height="170dp" 
      android:layout_marginTop="2dp" 
      android:layout_below="@id/datetime" 
      map:liteMode="true" 
      android:padding="10dp"/> 

     <LinearLayout 
      android:id="@+id/buttonLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_below="@id/map_listitem" 
      android:gravity="end" 
      android:layout_margin="4dp"> 

      <Button 
       android:backgroundTint="@color/colorPrimary" 
       android:textColor="@android:color/white" 
       android:id="@+id/acceptJob" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Accept"/> 

      <Button 
       android:backgroundTint="@color/darkRed" 
       android:textColor="@android:color/white" 
       android:id="@+id/declineJob" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Decline"/> 

     </LinearLayout> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 

我嘗試過各種各樣的事情,但似乎沒有奏效。

回答

3

嘗試增加的onResume,像這樣:

mapView.onCreate(null); 
mapView.getMapAsync(this); 
mapView.onResume(); 

編輯:
我剛纔注意到您在使用

map:liteMode="true" 

嘗試刪除它(至少測試)或增加以下爲xml

map:cameraZoom="15" 
map:mapType="normal" 
  • 無論哪種方式,我覺得是需要的onResume。
  • 當我使用liteMode時,有時需要幾秒鐘(約5分鐘)的地圖才能顯示徽標。
+0

沒有什麼區別。 – Asim

+0

你能夠發佈完整的代碼作爲一個項目(在Github等)? – MikeL

+0

不幸的是沒有。這項活動不應該足夠嗎?其他地方的地圖工作正常。 – Asim