Nutiteq:ViewLabel:延迟按钮执行

Nutiteq: ViewLabel: Delayed Button execution

我正在尝试创建一个包含三个 Button 的自定义视图的 nutiteq ViewLabel。 我的代码和问题与此非常相似 post nutiteq: Button resp. clickable Views not working in Custom ViewLabel.

根据所引用 post 提供的答案,我注意到调用了视图的 onTouchEvent-方法,并且执行点击的对象 post 由由 Choreographer 触发的 runQueue 的处理程序。

ViewLabel 中按下 Button 之后,我上面描述的那些内部调用发生了,但是 onClick-Button 的方法直到我按下 ViewLabel 之外的某处。然后 ViewLabel 关闭(就像它应该的那样,当按下它后面的地图时)并且 Toast 我放入 onClick - 方法被触发。

我认为问题与 UIThread 有关。似乎 performClick-Action 被放入了错误的 runQueue,当用户没有打开 Label 时它是活跃的。

如果有人有想法,如何解决该问题,那就太好了。

已编辑: 这是我构建 ViewLabel 的方式。此方法位于 class 中,由 MapActivity:

调用
    public void createViewMarker(View markerView, Bitmap icon, MapPos pos, Category cat, String tag) {
    MarkerStyle markerStyle = MarkerStyle.builder().setBitmap(icon).setSize(0.5f).setColor(
            Color.WHITE).build();
    ViewLabel label = new ViewLabel(markerView, new Handler(Looper.getMainLooper()));
    label.setTouchHandlingMode(true);
    markers.add(new MocaMarker(pos, label, markerStyle, cat,
            markerLayer, tag));
}

Nutiteq SDK 中的 ViewLabel 构造函数将 Handler 对象作为参数。它使用此处理程序来 post 标记重绘和触摸事件消息。首先,我会检查您的处理程序是否正确连接到 UI 线程。这里有关于此的其他信息:https://developer.android.com/training/multiple-threads/communicate-ui.html

google 组中的 nutiteq 团队回应后也没有解决方案( see here) 我决定完全摆脱 ViewLabel。

我现在的解决方案是将准备好的 poi 视图的布局包含到 MapActivity 布局文件中,并将其与我自定义的 MapListener 实现一起加载。 我在 MapListener 的构造函数中对 poi 视图元素进行了所有初始化,并在调用方法 onVectorElementClicked 时更改了可见性。这是它的基本外观的片段:

 @Override
public void onMapClicked(double x, double y, boolean b) {
    Toast.makeText(activity, "onMapClicked " + (new EPSG3857()).toWgs84(x, y).x + " " + (new EPSG3857()).toWgs84(x, y).y, Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onMapClicked " + x + " " + y);
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mPoiGlanceView.setVisibility(View.INVISIBLE);
        }
    });
}

@Override
public void onVectorElementClicked(VectorElement vectorElement, double v, double v2, boolean b) {
    if (vectorElement != null && vectorElement instanceof Marker) {
        showPopupView(vectorElement);
        Toast.makeText(activity, "onLabelClicked Marker!", Toast.LENGTH_SHORT).show();
    }
}

private void showPopupView(VectorElement vectorElement) {
    mPoiGlanceView.setVisibility(View.VISIBLE);
    setUpUiElementsForView();
}

nutiteq github 文档中的 ViewLabel 替代方案启发了我 see here "Use Android standard Layout tools"

对于那些仍想使用自定义视图的人,这是我找到的解决方法。我发现在自定义视图中触发的事件会在onMapMoved事件之后执行。因此,我手动触发它。这是代码:

YourViewID 替换为您的自定义视图布局 ID,将 YourButtonID 替换为按钮 ID。 将下面的代码添加到创建 ViewLabel

的位置
View v = getLayoutInflater().inflate(R.layout.YourViewID, null);
((Button)v.findViewById(R.id.YourButtonID)).setOnTouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        //Trigger only when users release the button
        if(motionEvent.getActionMasked()==MotionEvent.ACTION_UP){
            //Do your work
        }
        //Do not consume the OnTouch event
        return false;
    }
});

YourBaseLayoutID 替换为您的根布局 ID,并将 yourMapView 替换为您的 MapView 对象。 将下面的代码添加到 OnCreate().

findViewById(R.id.YourBaseLayoutID).setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(motionEvent.getActionMasked()==MotionEvent.ACTION_UP)
            yourMapView.getOptions().getMapListener().onMapMoved();
        return false;
    }
});