直播带货系统源码利用FloatingActionButton实现 展开/折叠多级悬浮菜单的相关代码
1.大家看一下,我们最终提供出来的调用的示例:
val expandFbItemList: MutableList<MultiFabItem> &#61; mutableListOf(MultiFabItem(...),MultiFabItem(...))val context &#61; LocalContext.currentMultiFloatingActionButton(srcIcon &#61; Icons.Filled.Add,showLabels &#61; true,items &#61; expandFbItemList) {Toast.makeText(context.applicationContext,"点击了:${it.label}",Toast.LENGTH_SHORT).show()}
2.我们需要实现展开和折叠功能&#xff0c;首先需要定义一个状态类
enum class MultiFabState {Collapsed,Expanded
}
3.我们需要外部能修改菜单内的数据&#xff0c;定义一个Item实体bean
class MultiFabItem(val icon: ImageVector,val label: String,val srcIconColor: Color &#61; Color.White,val labelTextColor: Color &#61; Color.White,val labelBackgroundColor: Color &#61; Color.Black.copy(alpha &#61; 0.6F),val fabBackgroundColor: Color &#61; Color.Unspecified,
)
4.定义一个MultiFloatingActionButton的Compose方法
&#64;Composable
fun MultiFloatingActionButton(modifier: Modifier &#61; Modifier,srcIcon: ImageVector,srcIconColor: Color &#61; Color.White,fabBackgroundColor: Color &#61; Color.Unspecified,showLabels: Boolean &#61; true,items: List<MultiFabItem>,onFabItemClicked: (item: MultiFabItem) -> Unit
)
5.定义动画&#xff0c;用于折叠和展开菜单显示的动画
val currentState &#61; remember { mutableStateOf(MultiFabState.Collapsed) }
val transition &#61; updateTransition(targetState &#61; currentState, label &#61; "")
val rotateAnim: Float by transition.animateFloat(.....) { state ->if (state.value &#61;&#61; MultiFabState.Collapsed) 0F else -45F
}
val alphaAnim: Float by transition.animateFloat(....) { state ->if (state.value &#61;&#61; MultiFabState.Expanded) 1F else 0F
}
val shrinkListAnim:MutableList<Float> &#61; mutableListOf()
items.forEachIndexed { index, _ ->val shrinkAnim by transition.animateFloat(.....)shrinkListAnim.add(index,shrinkAnim)
}
6.使用Box包裹我们的所有FloatingActionButton
Box(modifier &#61; modifier, contentAlignment &#61; Alignment.BottomEnd) {items.forEachIndexed{index, item ->Row(....){if(showLabels){Text(....)}FloatingActionButton(....)}}FloatingActionButton(....)
}
以上就是直播带货系统源码利用FloatingActionButton实现 展开/折叠多级悬浮菜单的相关代码&#xff0c; 更多内容欢迎关注之后的文章