English 中文(简体)
如何在Lazy Column找到目前app的物品
原标题:How to find current snapped item in Lazy Column
  • 时间:2024-01-05 13:39:36
  •  标签:
  • kotlin
问题回答

The SnapFlingBehavior itself knows no current item and is usually just provided to the scrollable modifier. The composable that this scrollable modifier is applied to must understand the concept of a current item, if this is something that you need.

堆肥框架提供内部处理所有这些问题的箱外堆肥,如<编码>PagerDatePicker

更通用的<代码>LazyList和LazyGrid,但可以滚动(因此,snapflingable),但它们对当前项目没有任何概念。 名单上所列国家的财产如下:firstVisibleItemIndex。 然而,在使用默认的<代码>SnapFlingBehavior(该编码可向观察中心发送)时,清单中的第一个项目往往只部分可见一斑。 您可以将SnapFlingBehavior定制成像,使第一个项目始终见到。 您必须为此建立自己的<代码>SnapLayoutInfoProvider(可能具有以下功能:<代码>SnapLayoutInfoProvider(LazyListState, SnapPositionInLayout),因此,您仅需要提供一种定制的<代码>SnapPositionInLayout。

如果您使用某种其他代用品(SnapFlingBehavior),则你必须确保这一代用品本身支持当前项目的概念。

你们可以这样作:

@Composable
fun rememberSnapFlingSelectedIndex(listState: LazyListState) = remember {
    derivedStateOf {
        with(listState) {
            val itemsOnScreen = layoutInfo.visibleItemsInfo.size.takeIf { it != 0 } ?: 1

            when {
                !canScrollBackward -> firstVisibleItemIndex
                !canScrollForward -> firstVisibleItemIndex + itemsOnScreen - 1
                else -> firstVisibleItemIndex + itemsOnScreen.div(2)
                // Or you can write your own conditions to determine item position
                // ...
            }
        }
    }

}




相关问题
Exposed runs query on all HikariCP open connections

I m running a Ktor server with a PostgreSQL database and just set up the JetBrains Exposed ORM framework with HikariCP for connection pooling (per the recommendation in the Ktor documentation). My ...

SQLite Kotlin issue - no database

I am trying to create boardgamegeek APIs based app in AndroidStudio, however from unknown reason I m getting my database created with no columns. Here s Logical log: (1) no such table: games in "...

Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...