gpt4 book ai didi

android - 如何将旋转滚动监听器附加到 Android Wear numberPicker?

转载 作者:行者123 更新时间:2023-12-02 10:36:15 24 4
gpt4 key购买 nike

我已经为此工作了一天,但仍然无法弄清楚。我正在为 Android Wear 设备制作 Android 应用程序,但我不知道如何检测设备上的转轮以获取滚动事件。我知道默认情况下,转轮会分配给 ScrollViews 和 Listviews 等,并且我已经让它可以工作,但我无法让它在简单的 NumberPicker 上工作。按照 Google API 的建议,我使用了 OnGenericMotionListener,虽然它没有抛出任何错误,但它仍然没有接收到任何内容。

我希望 NumberPicker 随滚轮滚动。我的代码如下。 logcat 中没有任何内容显示任何错误:

                MyActivity.this.setContentView(R.layout.number_picker);
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker1);
numberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
numberPicker.setMaxValue(100);
numberPicker.setMinValue(0);
numberPicker.setWrapSelectorWheel(true);
numberPicker.setOnValueChangedListener( new NumberPicker.
OnValueChangeListener() {
public void onValueChange(NumberPicker picker, int
oldVal, int newVal) {

}
});
numberPicker.setOnGenericMotionListener(new OnGenericMotionListener() {
@Override
public boolean onGenericMotion(View v, MotionEvent event) {

Log.i(TAG, "Received scroll event:" + event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL:

}
return false;
}
});

最佳答案

我认为您正在寻找这个:

Add custom rotary input scrolling :

If your scrollable view doesn't natively support rotary input scrolling, or if you want to do something other than scrolling in response to rotary input events (zoom in/out, turn dials, etc.), you can use the RotaryEncoder methods in the Wearable Support Library.

The following code snippet shows how to use RotaryEncoder to add custom scrolling in your app's view:

myView.setOnGenericMotionListener(new View.OnGenericMotionListener() {
@Override
public boolean onGenericMotion(View v, MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_SCROLL && RotaryEncoder.isFromRotaryEncoder(ev)) {
// Don't forget the negation here
float delta = -RotaryEncoder.getRotaryAxisValue(ev) * RotaryEncoder.getScaledScrollFactor(
getContext());

// Swap these axes if you want to do horizontal scrolling instead
scrollBy(0, Math.round(delta));

return true;
}

return false;
}
});

无论如何,你可以查看有关 android 中旋转事件的完整指南 磨损。

关于android - 如何将旋转滚动监听器附加到 Android Wear numberPicker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50382672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com