RecyclerView中的MapView仅在点击时显示地图



这一定是有史以来最荒谬的事情。

我有一个带有重复自定义项目的回收器视图。在这些项目中,有一些文本字段,按钮和一个MapView。

问题是,当列表加载时,MapView仅显示Google徽标,而不显示其他磁贴或详细信息(或标记(。但是,当我在地图上点击一次时,它会显示我添加的标记。在下一次点击时,它会加载一个像素化的地图。在另一个点击中,它会加载质量更好的地图。进一步单击时,它会为附近的位置添加文本标签。LatLngBounds也不起作用,但这是一个次要问题。

为什么会这样?

我的代码如下:

作业适配器.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>

我已经尝试了各种方法,但似乎没有任何效果。

尝试添加 onResume,如下所示:

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

编辑:
我刚刚注意到你正在使用

map:liteMode="true"  

尝试删除它(至少进行测试(或将以下内容添加到 xml 中:

map:cameraZoom="15"  
map:mapType="normal"
  • 无论哪种方式,我认为都需要 onResume。
  • 当我使用 liteMode 时,地图有时需要几秒钟(大约 5 秒(才能显示在徽标之后。

您的代码中可能存在另一个问题。"地图"应该是:

xmlns:map="http://schemas.android.com/apk/res-auto"

但不是:

xmlns:map="http://schemas.android.com/tools"

最新更新