【问题标题】:How to add Rotary Input into ScrollView in Android Wear如何在 Android Wear 中将旋转输入添加到 ScrollView
【发布时间】:2021-10-21 16:46:32
【问题描述】:

我正在开发一个 android wear 应用程序,我已经使用 rcView.requestFocus() 将旋转输入添加到 recyclerview,但它不适用于 NestedScrollview,所以我想知道如何将旋转输入侦听器添加到 NestedScrollview。

这是我到目前为止所做的事情

   binding.mainScroll.setOnGenericMotionListener { v, ev ->
            if (ev.action == MotionEvent.ACTION_SCROLL &&
                ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)
            ) {

                val delta = -ev.getAxisValue(MotionEventCompat.AXIS_SCROLL) *
                        ViewConfigurationCompat.getScaledVerticalScrollFactor(
                            ViewConfiguration.get(applicationContext), applicationContext
                        )
                
                v.scrollBy(0, delta.roundToInt())
                true
            } else {
                false
            }
        }

【问题讨论】:

  • 你有复制的例子吗?你可以通过在你自己的处理程序中使用 setOnGenericMotionListener developer.android.com/training/wearables/user-input/… 来解决它吗?
  • 我已经尝试过了,我更新了我的问题检查它,但 setOnGenericMotionListener 没有做任何事情
  • 你有一个最小的可重复的例子,它会帮助其他任何人试图提供帮助。
  • 你什么时候请求焦点?我已经看到其他视图有时会从您尝试设置的视图中窃取焦点的问题。您很可能希望在 onResume 中请求关注。有时添加延迟可以解决问题。它不漂亮,但它有效(大部分时间)

标签: android wear-os android-wear-2.0 android-wear-3.0


【解决方案1】:

在您的activity_layout 上,在ScrollView 中添加requestFocus。您需要您的 API 为 28 或更高。 示例:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <requestFocus
        android:focusable="true"
        android:focusableInTouchMode="true"/>
</ScrollView> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多