網站首頁 編程語言 正文
引言
DropdownMenu顯示時默認會避開點擊的view 通常默認顯示在左下方
本篇文章教你實現跟隨手指按下位置顯示
效果圖
實現方法
首先要獲取到點擊的位置之后計算偏移量
先分析兩種offset參數
1使用DropdownMenu的offset參數
獲取到點擊的位置之后計算偏移量, DropdownMenu的offset參數
@Composable fun DropdownMenu( ... offset: DpOffset = DpOffset(0.dp, 0.dp) ... )
這種方法比較麻煩,要反向計算偏移量,因為初始位置(x,y)軸的原點在左下角,而不是左上角
2Modifier.offset
將DropdownMenu嵌套在Box里面,調用BoxD的Modifier.offset()
改變DropdownMenu的顯示位置
這種方案的的初始位置(x,y)軸的原點在左上角
點擊的位置就是DropdownMenu的偏移量,
獲取到點擊的位置
層級結構
Box{ card() Box{ DropdownMenu() } }
Box創建用于監聽點擊事件修飾符
在最外層的Box創建一個用于監聽點擊事件的修飾符,來捕獲點擊位置
val animatedOffset = remember { Animatable(Offset(0f, 0f), Offset.VectorConverter) } Box( modifier = Modifier .fillMaxWidth() .pointerInput(Unit) { coroutineScope { while (true) { //獲取點擊位置 val boxOffset = awaitPointerEventScope { awaitFirstDown().position } //顯示DropdownMenu expanded = true launch { animatedOffset.animateTo( boxOffset, animationSpec = spring(stiffness = Spring.StiffnessLow) ) } } } } )
DropdownMenu外層的Box()設置偏移量
Box(modifier = Modifier.offset { IntOffset( animatedOffset.value.x.roundToInt(), animatedOffset.value.y.roundToInt() ) } ) { DropdownMenu() }
完整代碼
@OptIn(ExperimentalMaterial3Api::class) @Composable private fun FullContent( modifier: Modifier, context: String ) { var expanded by remember { mutableStateOf(false) } val animatedOffset = remember { Animatable(Offset(0f, 0f), Offset.VectorConverter) } Box( modifier = Modifier .fillMaxWidth() .pointerInput(Unit) { coroutineScope { while (true) { //獲取點擊位置 val boxOffset = awaitPointerEventScope { awaitFirstDown().position } expanded = true launch { animatedOffset.animateTo( boxOffset, animationSpec = spring(stiffness = Spring.StiffnessLow) ) } } } } ) { Card(modifier = modifier .fillMaxWidth(), border = BorderStroke(2.dp, CustomTheme.colors.divider) ) { Text( text = context, modifier = Modifier.padding(6.dp), fontSize = 17.sp, fontWeight = FontWeight.Normal, ) } Box(modifier = Modifier.offset { IntOffset( animatedOffset.value.x.roundToInt(), animatedOffset.value.y.roundToInt() ) } ) { DropdownMenu( expanded = expanded, onDismissRequest = { expanded = false } ) { DropdownMenuItem( text = { Text(stringResource(id = R.string.app_copy)) }, onClick = { }) DropdownMenuItem( text = { Text(stringResource(id = R.string.app_copy)) }, onClick = { }) Divider() DropdownMenuItem( text = { Text(stringResource(id = R.string.app_copy)) }, onClick = { }) } } } }
使用方法
FullContent( modifier = Modifier.fillMaxWidth(), context = "悄悄的我走了, 正如我悄悄的來; 我揮一揮衣袖, 不帶走一片云彩。" )
原文鏈接:https://juejin.cn/post/7111583959133192199
相關推薦
- 2023-07-10 NGINX使用rewrite報錯。
- 2021-12-15 go語言處理TCP拆包/粘包的具體實現_Golang
- 2022-05-15 Qt中QPixmap、QImage、QPicture、QBitmap四者區別詳解_C 語言
- 2022-02-04 ImportError: No module named ‘flask_sqlalchemy‘
- 2022-11-16 Python?讀取?.gz?文件全過程_python
- 2022-12-27 一文帶你了解Go語言標準庫strings的常用函數和方法_Golang
- 2022-05-10 關于react中的state整理
- 2022-11-23 linux下解決?git?clone每次都要輸入用戶名密碼問題(推薦)_linux shell
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支