0

我需要在我的程序中添加与用户平移/缩放 GraphView 相关的行为。我正在尝试注册运动事件以得到通知,因此我可以在用户通过触摸操作图形时做一些事情。

我已经尝试在 LineGraph 的子类中覆盖 onTouchEvent 并实现 OnTouchLIsener。我也尝试在放置 GraphView 的 Fragment / View 中执行此操作。但是,我的方法从未被调用,但该图允许像以前一样进行平移/缩放。

例如:

public CustomLineGraphView(Context context, String title) {
    super(context, title);
    this.setOnTouchListener(this);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.w("clg", "onTouchEvent()");
    return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.w("clg", "onTouch()");
    return false;
}
4

1 回答 1

0

您需要挂钩使用的底层View证券GraphView

假设您有一个名为 m_graphView 的 GraphView 对象,请执行以下操作。将 onTouchListener 附加到每个底层子级可能是最安全的,以防 GraphView 的实现在未来发生变化。

// attach OnTouchListener to the inner view that receives touch events for pan/zoom.
int childCount = m_graphView.getChildCount();
for(int index = 0; index < childCount; index++) {
   m_graphView.getChildAt(index).setOnTouchListener(new GraphTouchListener());
}

请务必在 onTouch() 中返回 false,以便平移/缩放的基类行为仍然有效。

private class GraphTouchListener implements View.OnTouchListener {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // do whatever custom behavior you want here
        // return false so that the base onTouch event (pan, zoom) can still execute.
        return false;
    }
}
于 2014-07-03T12:32:11.373 回答