根据单击侦听器更改infoWindow视图

时间:2015-05-03 14:01:46

标签: android google-maps-android-api-2

我正在尝试使用infoWindow实现Google地图标记,如果有人点击此infoWindow,它会播放一首歌,如果再次点击,则会停止播放。为了可视化,我编写了一个自定义的infoWindow布局。在infoWindow中,您可以通过按钮查看用户和跟踪信息。如果曲目尚未开始播放,此按钮会显示播放图标,如果按下该按钮(按下infoWindow,而不是按钮),我希望它将其图标从“播放”更改为“停止”。但是,我无法根据infoWindowClickListener活动更改自定义infoWindow的视图。我试图更改infoWindowAdapter,但我不想更改所有其他infoWindows的视图,我也希望立即看到更改。通过这种方式,infoWindow再次单击标记后刷新其视图。换句话说,它不会与我的点击操作同时更改视图。

在这里你可以看到我在说什么。停止左侧状态,右侧播放状态:

enter image description here

这是我对适配器徒劳的努力:

public class OrangeInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    Context context;
    ImageButton playButton;
    boolean onPlay;

    public OrangeInfoWindowAdapter(Context context, boolean onPlay) {
        this.context = context;
        this.onPlay = onPlay;
    }

    @Override
    public View getInfoWindow(Marker arg0) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.orange_infowindow, null);

        v.setMinimumWidth(280);
        v.setMinimumHeight(120);

        TextView tvUsername = (TextView) v.findViewById(R.id.tv_username);
        TextView tvTrack = (TextView) v.findViewById(R.id.tv_track);

        int index = arg0.getTitle().indexOf("*");

        try {
            tvUsername.setText(arg0.getTitle().substring(0, index - 1) + "\n" + arg0.getTitle().substring(index + 2));
        } catch (StringIndexOutOfBoundsException e) {
        }

        tvUsername.setTextSize(10);
        tvUsername.setTextColor(Color.rgb(70, 70, 70));

        index = arg0.getSnippet().indexOf("*");

        try {
            tvTrack.setText(arg0.getSnippet().substring(0, index - 1) + "\n" + arg0.getSnippet().substring(index + 2));
        } catch (StringIndexOutOfBoundsException e) {
        }

        tvTrack.setTextSize(10);
        tvTrack.setTextColor(Color.rgb(230, 92, 1));

        playButton = (ImageButton) v.findViewById(R.id.playButton);

        if (onPlay)
            onPlay();

        return v;

    }

    public void onPlay() {
        playButton.setBackgroundResource(R.drawable.info_stop_button);
    }

    public void onStop() {
        playButton.setBackgroundResource(R.drawable.info_play_button);
    }

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

这是我的onInfoWindowClick():

@Override
public void onInfoWindowClick(Marker marker) {
    if (!infoWindowPlayerActive) {
        int index = findMarkerIndex(marker);
        OrangeInfoWindowAdapter infoWindowAdapter2 = new OrangeInfoWindowAdapter(getActivity().getApplicationContext(), true);
        googleMap.setInfoWindowAdapter(infoWindowAdapter2);
        new InfoWindowPlayerTask(mainActivity).execute(activities.get(index).getTrackId());
        infoWindowPlayerActive = true;
    }

    else {
        // same thing...
        infoWindowPlayerActive = false;
    }
}

如果您想了解更多信息以便清楚地了解问题,请与我联系。

0 个答案:

没有答案