使用Compose Android Destination库实现共享元素过渡的疑问
Compose Destinations 共享元素过渡所需参数
要实现共享元素过渡,核心是传递共享元素的唯一标识Key,配合库的API完成前后页面元素的关联,具体细节如下:
起始页:给需要共享的元素添加
sharedContent修饰符,指定一个唯一的key(通常用业务数据的ID拼接,保证唯一性):Image( painter = painterResource(id = item.photoRes), contentDescription = null, modifier = Modifier .sharedContent( key = "shared_photo_${item.id}", state = rememberSharedContentState() ) )目标页:在对应共享元素上使用完全相同的key,无需额外传递过渡专用参数:
Image( painter = painterResource(id = receivedItem.photoRes), contentDescription = null, modifier = Modifier .sharedContent( key = "shared_photo_${receivedItem.id}", // 和起始页key一致 state = rememberSharedContentState() ) )业务数据传递:如果目标页需要展示的内容(比如图片资源、文本)需要从起始页传递,直接用库支持的序列化类、基本类型(字符串、布尔值等)传递即可,这部分和普通页面跳转传参逻辑一致,不影响过渡效果。
本质上,共享元素过渡只依赖前后页面元素使用相同的唯一Key来建立关联,其他参数都是为业务逻辑服务的常规传参。
内容的提问来源于stack exchange,提问作者Gaurav Gujjar
相关产品推荐
相关产品推荐

