简体   繁体   English

Jetpack Compose 中的 Android 键盘导航

[英]Android Keyboard Navigation in Jetpack Compose

I'm trying to implement keyboard navigation in Jetpack Compose.我正在尝试在 Jetpack Compose 中实现键盘导航。

Have a LazyColumn which contains some list elements as Row{} all the Row's are clickable and receive focus.有一个LazyColumn ,其中包含一些列表元素作为Row{} ,所有 Row 都是可点击的并获得焦点。 What I'm trying to achieve is to scroll to next item in the list after reaching the bottom of the screen.我想要实现的是在到达屏幕底部后滚动到列表中的下一项。

After pressing the DOWN arrow key / TAB key, the focus stops at the bottom of the screen and does not scroll down.按向下箭头键/TAB 键后,焦点停止在屏幕底部,不会向下滚动。 Am I wrong to expect it to happen automatically?我期望它自动发生是错误的吗? Do we need to use key events and how can we use key events to support scrolling using keyboard with Jetpack Compose.我们是否需要使用键事件以及如何使用键事件来支持使用 Jetpack Compose 使用键盘进行滚动。

Code:代码:

LazyColumn(
            modifier = Modifier
                .padding(horizontal = Spacing2X)
                .padding(top = Spacing2X)
        ) {
            items(SOME_LIST_HERE) {
                Row(modifier = Modifier.clickable { }) {
                    Text(text = "")
                }
            }
        }

Scenario Video: https://twitter.com/karan4c6/status/1479426842566094849场景视频: https://twitter.com/karan4c6/status/1479426842566094849

You will have to use lazyListState to achieve that.您将不得不使用lazyListState来实现这一点。 First, you need to create it using val state = rememberLazyListState() and then provide it to your LazyColumn as a parameter.首先,您需要使用val state = rememberLazyListState()创建它,然后将其作为参数提供给您的LazyColumn

You then need to use state.layoutInfo.visibleItemsInfo on every focus change to see what items are currently visible, combine that knowledge with state.firstVisibleItemIndex and which item is currently focused, and use it to scroll accordingly.然后,您需要在每次焦点更改时使用state.layoutInfo.visibleItemsInfo以查看当前可见的项目,将这些知识与state.firstVisibleItemIndex和当前聚焦的项目结合起来,并使用它来相应地滚动。

It's a hacky solution involving handling of indexes, but I made it work.这是一个涉及处理索引的 hacky 解决方案,但我成功了。 I've lost access to that project since, so I cannot provide you with the working example, sorry.从那以后我就无法访问该项目,所以我无法为您提供工作示例,抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM