我的视图如何响应鼠标滚轮? [英] How can my view respond to a mousewheel?

查看:81
本文介绍了我的视图如何响应鼠标滚轮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有onTouch的视图,该视图可以区分触摸输入和左/中/右鼠标单击,如

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

我也希望此视图响应鼠标滚轮. onTouch不是这样,我也没有找到任何其他事件处理程序来响应鼠标滚轮.也许视图可以假装为可滚动的并且可以使用滚动方法来做自己的事?此时,我已经放弃了,正在使用键盘输入(1到9,再加上0)来选择显示的元素,这些元素我希望用鼠标滚轮选择.
因此,请务必提供可靠的提示或少量代码.

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.

不要担心会向公众强行要求使用键盘和鼠标的Android UI;该应用程序是一种开发工具.

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

下面给出了正确的答案,但是这个问题对以后的读者来说更有用,这是(略作编辑)我正在使用的实际代码:

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

推荐答案

可接受的答案是一个文档链接,该链接使我进入问题中的示例代码,但该答案已被删除.这样这个问题就不会再出现未回答"了,这就是您的视图如何响应鼠标滚轮的情况:

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

这篇关于我的视图如何响应鼠标滚轮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆