Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

android - How can my view respond to a mousewheel?

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...