【问题标题】:Adding swipe gesture handling to a Compose View将滑动手势处理添加到撰写视图
【发布时间】:2019-11-16 14:46:46
【问题描述】:

找不到任何示例代码。因为我没有看到任何手势类,所以我只是去了常规文档,而那些集中在将 Activity 设置为GestureRecognizerCompat,这不起作用。设置它可以工作,但不会检测到滑动或点击(在模拟器中)。在 SwiftUI 中有 Gestures 的处理程序。在 Compose Slack Channel 上询问并被告知寻找 FooGestureDetector 可组合函数。

寻找一些示例代码。终于有一个 Compose 教程了,它本来就很好,但是当你完成后你的应用程序做的很少。我还查看了 GitHub 上的示例项目,该项目用于为新闻网站创建帖子。有一些有趣的东西,但没有手势。

主要目标只是拥有多个 Cards 并能够在它们之间滑动(例如 ViewPager(1/2),这在 Compose 中还没有)。

我确实设法检测到对视图的点击,如下所示:

@Composable
fun PreviewPane(name: String, info:String) {
    val pagenumber = +state { 0 }

    CustomTheme{
        Column(
            crossAxisSize = LayoutSize.Expand,
            modifier = Spacing(16.dp)
        ) {
        Clickable(onClick = { Log.d("DEMO", "click detected..")}) {
            Text(text = name, style = TextStyle(fontWeight = FontWeight.Bold))
            Text(text = info)
            Text(text = "${pagenumber.value}")
        }

        Row {
            Button(text = "Back", onClick = {
                if (pagenumber.value > 0) pagenumber.value--
                Log.d("DEMO", "button clicked..")
            })
            Button(text = "Next", onClick = {
                pagenumber.value++
                Log.d("DEMO", "button clicked..")
            })
        }
    }

    }
}

这可行,而且我确实看到了日志消息(注意我没有用Click 指令包装按钮)。但我真正想要的是对包含所有这些组件的外部视图进行滑动检测。

【问题讨论】:

    标签: android kotlin android-jetpack-compose


    【解决方案1】:

    @Rob 如果您只需要查看寻呼机,您可以查看 Gist 的 ViewPager,我为 ViewPager 创建了一个示例,请从 Gist 和下面的示例中复制 ComposeViewPager.kt.kt。

    注意:我从Leland RichardsonGit repo 获取此代码并根据我的需要进行修改。

    例如:

    val listItem = arrayListOf<String>("One", "Two", "Three", "Four", "Five")
    
    @Composable
    fun FullViewPagerSample() {
        Box {
            ViewPager(
                items = listItem
            ) { listItem, itemWidthInDp ->
                ViewItem(
                    item = listItem,
                    normalWidth = itemWidthInDp
                )
            }
        }
    }
    
    
    @Composable
    fun ViewPagerSample() {
        val configuration = ConfigurationAmbient.current
        val screenWidth = configuration.screenWidthDp.dp
        val posterWidthDp = screenWidth * 0.6f
        Box {
            ViewPager(
                items = listItem, width = posterWidthDp
            ) { listItem, itemWidthInDp ->
                ViewItem(
                    item = listItem,
                    normalWidth = itemWidthInDp
                )
            }
        }
    }
    
    @Composable
    fun ViewPagerSampleWithYDeltaAnimation() {
        val configuration = ConfigurationAmbient.current
        val screenWidth = configuration.screenWidthDp.dp
        val posterWidthDp = screenWidth * 0.6f
        Box {
            ViewPager(
                items = listItem, width = posterWidthDp, bottomYDelta = 50f
            ) { listItem, itemWidthInDp ->
                ViewItem(
                    item = listItem,
                    normalWidth = itemWidthInDp
                )
            }
        }
    }
    
    @Composable
    fun ViewItem(
        item: String,
        normalWidth: Dp,
        modifier: Modifier = Modifier,
        isScrollable: Boolean = true
    ) {
        ScrollableColumn(
            modifier = modifier
                .width(normalWidth)
                .padding(10.dp)
                .clip(RoundedCornerShape(10.dp))
                .background(Color.Red),
            contentPadding = PaddingValues(posterPadding),
            isScrollEnabled = isScrollable,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                item,
                fontSize = 24.sp,
                color = Color.White
            )
        }
    }
    

    结果会是这样的,

    【讨论】:

    • 如何连接到标签页?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 2023-04-10
    相关资源
    最近更新 更多