GoogleMaps InfoWindow 延迟加载信息

问题描述

我使用 Firebase 来存储标记数据并在 GoogleMaps 中显示它们。在我创建 InfoWindowAdapter 以显示标记标题和图像之后。我使用这种方法在 InfoWindow 中显示标题和图像:

public class MapInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private final View infoWindow;
    private Context mContext;
    DatabaseReference databaseReference;
    Query query;

    public MapInfoWindowAdapter(Context context) {
        mContext = context;
        infoWindow = LayoutInflater.from(context).inflate(R.layout.map_info_window,null);
    }

    private void renderViewText(Marker marker,View view) {
        String title = marker.getTitle();
        TextView tvTitle = (TextView) view.findViewById(R.id.mapName);
        ImageView imageView = (ImageView) view.findViewById(R.id.mapImageView);

        databaseReference = FirebaseDatabase.getInstance().getReference("location");
        query = databaseReference.orderByChild("title").equalTo(title);

        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot usersnapshot : snapshot.getChildren()) {
                    String infoTitle = usersnapshot.child("title").getValue().toString();
                    String infoImage = usersnapshot.child("image").getValue().toString();
                    Glide.with(imageView).load(infoImage).into(imageView);
                    tvTitle.setText(infoTitle);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });

    }

    @Override
    public View getInfoWindow(@NonNull Marker marker) {
        renderViewText(marker,infoWindow);
        return infoWindow;
    }

    @Override
    public View getInfoContents(@NonNull Marker marker) {
        renderViewText(marker,infoWindow);
        return infoWindow;
    }
}

我的问题是,点击标记后,它会显示认图像和标题(设置为 TextView 和 ImageView)。只有在第二次按下后才会显示正确的信息。我必须按每个标记两次才能显示正确的信息,第一次按打开 InfoWindow,第二次按同一标记加载正确的数据。第一次按下后如何让它显示正确的数据?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)