How can my view respond to a mousewheel?

拟墨画扇 提交于 2020-01-31 04:23:06

问题


I have a view with an onTouch that can distinguish between touch input and left/middle/right mouse clicks, as in

@Override
public boolean onTouch(View v, MotionEvent event) {
  if (event.getButtonState() == MotionEvent.BUTTON_PRIMARY) // API 14 required
    ...
  ...
}

I'd also like this view to respond to the mousewheel. onTouch is not the way, nor have I found any other event handler to respond to the mousewheel. Maybe the view can pretend to be scrollable and do its own thing with scrolling methods? At this point, I've given up, and am using keyboard input (1 through 9, plus 0) to select displayed elements that I'd prefer to select with the mousewheel.
So a firm hint or a bit of code would be appreciated.

Don't worry that an Android UI requiring a keyboard and mouse will be inflicted on the public; the app is a development tool.

EDIT: the correct answer is given below, but just so this question is more helpful to future readers, this is (slightly edited) the actual code that I'm using as a result:

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
  if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_SCROLL:
        if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f)
          selectNext()
        else
          selectPrev();
        return true;
    }
  }
  return super.onGenericMotionEvent(event);
}

回答1:


The accepted answer was a document link that led me to the example code in the question, but that answer was deleted. So that this question no longer appears 'unanswered', this is how your view can respond to a mousewheel:

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
  if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_SCROLL:
        if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f)
          selectNext();
        else
          selectPrev();
        return true;
    }
  }
  return super.onGenericMotionEvent(event);
}



回答2:


The mouse wheel event action is considered a scroll event



来源:https://stackoverflow.com/questions/11024809/how-can-my-view-respond-to-a-mousewheel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!