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

4

我正在开发一个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
            }
        }

1
你有一个示例复制品吗?你能通过使用setOnGenericMotionListener来连接自己的处理程序来解决它吗?https://developer.android.com/training/wearables/user-input/rotary-input - Yuri Schimke
我已经尝试过了,我更新了我的问题,请检查一下,但setOnGenericMotionListener没有起作用。 - Sarath Siva
你有一个最小可重现的例子吗?这将有助于其他人尝试帮助。 - Yuri Schimke
你什么时候请求焦点?我曾经遇到过其他视图有时会从你想要设置焦点的视图中夺走焦点的问题。你最好在onResume中请求焦点。有时添加延迟可以解决问题。虽然不太美观,但它有效(大部分时间)。 - TofferJ
1个回答

7
在你的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> 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接