如何在InfoWindowAdapter中设置click事件

时间:2014-10-30 10:30:58

标签: android map google-maps-markers

我在地图上工作我创建了一个地图屏幕并使用了marker.markers是在Info Window Adapter的帮助下创建的。在Info Window适配器中我有textview名称,roll no和地址我想在名称,地址上设置click事件。你能告诉我这是怎么做的。

package com.ihealthhome.ui;
import java.util.Hashtable;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.model.Marker;
import com.ihealthhome.R;
import com.ihealthhome.dataparser.ClientsDetailDAO;
import com.ihealthhome.listeners.ClientDetailNavigationListener;

/**
 * @author DotZoo Inc, created on 02-Jan-2014
 */
public class CustomInfoWindowAdapter implements InfoWindowAdapter {

    private View view;
    Context ctx;
    private Hashtable<String, ClientsDetailDAO> renderMarker;

    public CustomInfoWindowAdapter(Context ctx,
            Hashtable<String, ClientsDetailDAO> markers) {
        this.ctx = ctx;
        this.renderMarker = markers;
        view = ((Activity) ctx).getLayoutInflater().inflate(
                R.layout.custom_infowindow, null);
    }

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

    @Override
    public View getInfoWindow(Marker marker) {

        final ImageView profileImage = (ImageView) view
                .findViewById(R.id.userProfile);
        TextView clientName = ((TextView) view.findViewById(R.id.clientName));
        final TextView clientCellPhone = ((TextView) view
                .findViewById(R.id.clientCellPhone));
        final TextView clientHomePhone = ((TextView) view
                .findViewById(R.id.clientHomePhone));
        final TextView clientWorkPhone = ((TextView) view
                .findViewById(R.id.clientWorkPhone));
        final TextView clientDashboard = ((TextView) view
                .findViewById(R.id.clientDashboard));

        final ClientsDetailDAO info = renderMarker.get(marker.getId());

        String firstName = info.getFirstName();
        String lastname = info.getLastName();
        if (!TextUtils.isEmpty(firstName) && !TextUtils.isEmpty(lastname)) {
            clientName.setText(new StringBuilder(firstName).append(" ").append(
                    lastname));
        }

        String cellPhone = info.getCellPhone();
        if (cellPhone != null && !TextUtils.isEmpty(cellPhone)) {
            clientCellPhone.setText(cellPhone);
        }

        String homePhone = info.getHomePhone();
        if (homePhone != null && !TextUtils.isEmpty(homePhone)) {
            clientHomePhone.setText(homePhone);
        }

        String workPhone = info.getWorkPhone();
        if (workPhone != null && !TextUtils.isEmpty(workPhone)) {
            clientWorkPhone.setText(workPhone);
        }

        clientName.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(ctx, "on click", Toast.LENGTH_LONG).show();
                mClientDetailListener.gotoClientDetail(info.getClientID(),
                        info.getFirstName());

            }
        });

        return view;
    }

    ClientDetailNavigationListener mClientDetailListener;

    public void setNavigateToClientDetailListener(
            ClientDetailNavigationListener mClientDetailListener) {
        this.mClientDetailListener = mClientDetailListener;
    }
}

1 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

您必须将setOnTouchListener与自定义触控侦听器一起使用。

public abstract class OnInfoWindowElemTouchListener implements OnTouchListener {
    private final View view;
    private final Handler handler = new Handler();

    private Marker marker;
    private boolean pressed = false;

    public OnInfoWindowElemTouchListener(View view) {
        this.view = view;
    }

    public void setMarker(Marker marker) {
        this.marker = marker;
    }

    @Override
    public boolean onTouch(View vv, MotionEvent event) {
        if (0 <= event.getX() && event.getX() <= view.getWidth() &&
            0 <= event.getY() && event.getY() <= view.getHeight())
        {
            switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN: startPress(); break;

            // We need to delay releasing of the view a little so it shows the pressed state on the screen
            case MotionEvent.ACTION_UP: handler.postDelayed(confirmClickRunnable, 150); break;

            case MotionEvent.ACTION_CANCEL: endPress(); break;
            default: break;
            }
        }
        else {
            // If the touch goes outside of the view's area
            // (like when moving finger out of the pressed button)
            // just release the press
            endPress();
        }
        return false;
    }

    private void startPress() {
        if (!pressed) {
            pressed = true;
            handler.removeCallbacks(confirmClickRunnable);
            if (marker != null) 
                marker.showInfoWindow();
        }
    }

    private boolean endPress() {
        if (pressed) {
            this.pressed = false;
            handler.removeCallbacks(confirmClickRunnable);
            if (marker != null) 
                marker.showInfoWindow();
            return true;
        }
        else
            return false;
    }

    private final Runnable confirmClickRunnable = new Runnable() {
        public void run() {
            if (endPress()) {
                onClickConfirmed(view, marker);
            }
        }
    };

    /**
     * This is called after a successful click 
     */
    protected abstract void onClickConfirmed(View v, Marker marker);
}

clientName.setOnTouchListener(new OnInfoWindowElemTouchListener() {
    @Override
    protected void onClickConfirmed(View v, Marker marker) {
        Toast.makeText(ctx, "on click", Toast.LENGTH_LONG).show();                           
        mClientDetailListener.gotoClientDetail(info.getClientID(),info.getFirstName());
    }
});