android 如何在detectTapGestures中获取firstvisibleItemIndex

2izufjch  于 2023-05-15  发布在  Android
关注(0)|答案(1)|浏览(122)

我有一个lazycolumn,使用detectTapGestures获取onclick事件
我想在单击Lazycolumn项时获取Lazycolumn的当前firstvisibleItemIndex
尝试了不同的解决方案与状态.firstVisibleItemIndex但没有工作-我如何才能开始?

Card(colors = CardDefaults.cardColors(
        containerColor = Color(0xFF7DCEA0), Color.Black
    ),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
        modifier = Modifier
            .padding(horizontal = 6.dp, vertical = 6.dp)
            .offset { IntOffset(swipeableState.offset.value.roundToInt(), 0) }
            .pointerInput(Unit) {

                detectTapGestures(

                    onLongPress = {
                     //   val firstVisibleItemIndex = state.firstVisibleItemIndex

                      
                        mContext.startActivity(Intent(mContext, LKList::class.java))
                    },

                    onTap = {
                        Log.e(
                            "TestX",
                            ">>>>>> DCMainItem : On Tap"
                        )
                      
                        mContext.startActivity(Intent(mContext, DCInfo::class.java))
                    }
                )
kkih6yb8

kkih6yb81#

您可以使用类似于:

val listState = rememberLazyListState()

    LazyColumn(state = listState) {

        items(itemsList) {
            Text("Item is $it",
                modifier = Modifier
                    .pointerInput(Unit) {
                        detectTapGestures(
                            onLongPress = {
                                val firstVisibleItemIndex = listState.firstVisibleItemIndex
                                //....
                            },
                        )
                    }
            )
        }
    }

相关问题