2

I am working on an android wear app, I already added rotary input to recyclerview using rcView.requestFocus(),but it doesn't work with NestedScrollview so I want to know how to add the rotary input listener to NestedScrollview.

Here is what I have done so far

   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
            }
        }
Sarath Siva
  • 547
  • 3
  • 14
  • 1
    Do you have an example reproduction? Can you workaround it by wiring in your own handler with setOnGenericMotionListener https://developer.android.com/training/wearables/user-input/rotary-input ? – Yuri Schimke Oct 21 '21 at 19:41
  • I already tried that, i updated my question check it, but setOnGenericMotionListener is not doing anything – Sarath Siva Oct 22 '21 at 10:58
  • Have you got a minimal reproducible example, it would help with anyone else trying to help. – Yuri Schimke Oct 22 '21 at 11:30
  • When are you requesting focus? I've seen issues where other views sometimes steals the focus from the view you're trying to set it too. You most likely want to request focus in onResume. Sometimes adding a delay does the trick. It's not pretty but it works (most of the time) – TofferJ Oct 28 '21 at 15:26

1 Answers1

3

On your activity_layout add requestFocus inside the ScrollView. You need your API to be 28 or higher. Example:

<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> 
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46