水平分页器内的 LazyColumn 未按预期工作

问题描述 投票:0回答:1

我在水平寻呼机内使用惰性列,但是当我滚动并想要立即单击项目时,没有任何反应(这里是视频:https://www.youtube.com/shorts/oDlHoHe1tN8)。这是代码:

 HorizontalPager(
                        state = pagerState!!,
                        modifier = Modifier.fillMaxHeight()
                    ) {
                            AssetsListView(
                                            assetsList = currentAssets.data ?: emptyList(),
                                            modifier = Modifier.fillMaxSize(),
                                            onRowSelected = { asset ->
                                                callBacks.onCoinSelected(asset, false)
                                            },
                                            baseAsset = marketBaseAsset,
                                        )
}

@Composable
fun AssetsListView(
    modifier: Modifier = Modifier,
    onRowSelected: (Asset) -> Unit = {},
    assetsList: List<Asset>,
    baseAsset: BaseAsset = BaseAsset.IRT,
) {
    CompositionLocalProvider(LocalRippleTheme provides SelectRippleTheme) {
        LazyColumn(modifier = modifier) {
            itemsIndexed(
                assetsList,
                key = { index, value -> value.id }) { index, value ->
                val onItemSelected = remember {
                    { onRowSelected(value) }
                }
                if(value.symbol != BaseAsset.IRT.symbol) {
                    CoinInfoRow(
                        asset = value,
                        modifier,
                        onRowClicked = onItemSelected,
                        baseAsset = baseAsset
                    )
                }
            }
        }
    }
}
Do you know why this keeps happening? The same issue can be found in the "Sunflower" app also.
android user-interface mobile android-jetpack-compose android-jetpack
1个回答
0
投票

您可以编写自己的可点击事件,通过更改 pass 来在使用 PointerInput 滚动之前调用该事件。

我发布的内容没有任何指示,但如果需要,也可以在这个答案中添加。

@Preview
@Composable
fun ClickPassTest() {
    val pagerState = rememberPagerState {
        3
    }

    val context = LocalContext.current

    val interactionSourceList = remember {
        List(50) {
            MutableInteractionSource()
        }
    }

    HorizontalPager(state = pagerState) {
        LazyColumn(
            modifier = Modifier.fillMaxSize()
        ) {

            items(50) {
                Text(
                    modifier = Modifier
                        .fillMaxWidth()
                        .indication(
                            interactionSource = interactionSourceList[it],
                            ripple()
                        )
                        .clickableCustom(interactionSource = interactionSourceList[it])
                        {
                            Toast
                                .makeText(context, "CUSTOM Clickable", Toast.LENGTH_SHORT)
                                .show()
                        }
                        .padding(16.dp),
                    text = "Row $it"
                )
            }
        }
    }
}

fun Modifier.clickableCustom(
    pass: PointerEventPass = PointerEventPass.Main,
    interactionSource: MutableInteractionSource? = null,
    onClick: () -> Unit
) = this.then(
    Modifier.pointerInput(Unit) {
        coroutineScope {
            awaitEachGesture {
                val down = awaitFirstDown(requireUnconsumed = false, pass = pass)

                val pressInteraction = PressInteraction.Press(down.position)
                launch {
                    interactionSource?.emit(pressInteraction)
                }

                val up = waitForUpOrCancellation(pass = pass)

                if (up != null) {
                    launch {
                        interactionSource?.emit(PressInteraction.Release(pressInteraction))
                    }
                    onClick()
                } else {
                    launch {
                        interactionSource?.emit(PressInteraction.Cancel(pressInteraction))
                    }
                }
            }
        }
    }
)
© www.soinside.com 2019 - 2024. All rights reserved.