【问题标题】:how to set click event in InfoWindowAdapter如何在 InfoWindowAdapter 中设置点击事件
【发布时间】:2014-10-30 10:30:57
【问题描述】:

我正在处理地图我已经创建了一个地图屏幕并使用marker.markers 是在Info Window Adapter 的帮助下创建的。在信息窗口适配器中,我有文本视图名称、卷号和地址,我想在名称、地址上设置点击事件。你能告诉我这是怎么做到的吗?

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;
    }
}

【问题讨论】:

  • 尝试将每个视图点击监听器实现为clientName。
  • 你能解释一下吗?
  • 点击命名等时你想做什么?
  • 我想在 clientName 上打开 ClientDetailActivity 点击并点击 profileImage 我想打开另一个活动。
  • 尝试使用 Intent intent = new Intent(ctx,ClientDetailActivity.class); ctx.startActivity(intent)

标签: android map google-maps-markers


【解决方案1】:

试试这个方法,希望能帮助你解决问题。

您必须将 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());
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    相关资源
    最近更新 更多