Android GoogleMap 2通过Universal Image loader在ImageWindow中使用ImageView动态更新信息

时间:2013-04-22 10:02:41

标签: android android-maps-v2 universal-image-loader

我正在开发新的Google Map v2 API。

我使用ImageLoader在Marker上动态显示图像。

但问题是当我使用Universal Image Loader的onLoadingComplete()时,ImageView既不会自动也不会手动失效。

下次如果我点击相同的标记,图像将显示在通用图像加载器的缓存中。

以下是我的 CustomInfoWindowAdapter 代码:

private class CustomInfoWindowAdapter implements InfoWindowAdapter {

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        final View mWindow = getLayoutInflater().inflate(R.layout.custom_info_window, null);

        render(marker, mWindow);
        return mWindow;
    }

    private void render(final Marker marker, View view) {

        final String url = markers.get(marker.getId()).getStrProfilePic();
        final ImageView image = ((ImageView) view.findViewById(R.id.badge));
        Log.e(TAG, "URL : " + url);

        if ( url != null && !url.equalsIgnoreCase("null")
                && !url.equalsIgnoreCase("")) {
            imageLoader.displayImage(url, image, options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view,
                        Bitmap loadedImage) {
                    super.onLoadingComplete(imageUri, view, loadedImage);
                    ((ImageView) view).invalidate();
                }
            });
        } else {
            image.setImageResource(R.drawable.noimage);
        }

        final String title = marker.getTitle();
        final TextView titleUi = ((TextView) view.findViewById(R.id.title));
        if (title != null) {
            titleUi.setText(title);
        } else {
            titleUi.setText("");
        }

        final String snippet = marker.getSnippet();
        final TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
        if (snippet != null) {
            snippetUi.setText(snippet);
        } else {
            snippetUi.setText("");
        }
    }
}

当我添加Marker时,我将Marker Ids存储在HashTable中。从他们那里我将得到照片的URL。

2 个答案:

答案 0 :(得分:9)

我在类级别使用了一个Marker对象,并使用该对象更新了Marker信息。

我已按照以下方式自定义我的类来更新ImageView:

private class CustomInfoWindowAdapter implements InfoWindowAdapter {

    private View view;

    public CustomInfoWindowAdapter() {
        view = getLayoutInflater().inflate(R.layout.custom_info_window, null);          
    }

    @Override
    public View getInfoContents(Marker marker) {

        if ( YourClassName.this.marker != null &&
                ClassName.this.marker.isInfoWindowShown() ) {
            YourClassName.this.marker.hideInfoWindow();
            YourClassName.this.marker.showInfoWindow();
        }
        return null;
    }

    @Override
    public View getInfoWindow(final Marker marker) {
        YourClassName.this.marker = marker;

        final String url = markers.get(marker.getId()).getStrProfilePic();
        final ImageView image = ((ImageView) view.findViewById(R.id.badge));

        if ( url != null && !url.equalsIgnoreCase("null")
                && !url.equalsIgnoreCase("")) {
            imageLoader.displayImage(url, image, options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view,
                        Bitmap loadedImage) {
                    super.onLoadingComplete(imageUri, view, loadedImage);
                    getInfoContents(marker);
                }
            });
        } else {
            image.setImageResource(R.drawable.noimage);
        }

        final String title = marker.getTitle();
        final TextView titleUi = ((TextView) view.findViewById(R.id.title));
        if (title != null) {
            titleUi.setText(title);
        } else {
            titleUi.setText("");
        }

        final String snippet = marker.getSnippet();
        final TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
        if (snippet != null) {
            snippetUi.setText(snippet);
        } else {
            snippetUi.setText("");
        }

        return view;
    }
}

Check the Example

答案 1 :(得分:3)

从信息窗口返回的视图回调后无法更新。

请尝试遵循以下建议:Dynamic contents in Maps V2 InfoWindow