如何在 Android Wear 中的 ScrollView 中添加旋转输入

Sar*_*iva 5 android android-wear-2.0 wear-os android-wear-3.0

我正在开发一个 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
            }
        }
Run Code Online (Sandbox Code Playgroud)

小智 8

在您activity_layout添加requestFocusScrollView. 您需要 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> 
Run Code Online (Sandbox Code Playgroud)