CV工程师
2024-06-13 07:52:40 阅读:593
在 Jetpack Compose 中监听 Android 底部导航栏的显示与隐藏可以通过使用系统UI控制器实现。以下是一个简单的示例代码:
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedDispatcher
import androidx.compose.runtime.*
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.platform.LocalViewCompositionStrategy
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
@Composable
fun BottomNavBarVisibilityListener(
onBottomNavBarVisibilityChanged: (Boolean) -> Unit
) {
val context = LocalContext.current
val view = LocalView.current
DisposableEffect(key1 = view) {
val activity = context as ComponentActivity
val onApplyWindowInsetsListener = ViewCompat.OnApplyWindowInsetsListener { _, insets ->
val isVisible = insets.isVisible(WindowInsetsCompat.Type.navigationBars())
onBottomNavBarVisibilityChanged(isVisible)
insets
}
ViewCompat.setOnApplyWindowInsetsListener(view, onApplyWindowInsetsListener)
val onDestory = {
ViewCompat.setOnApplyWindowInsetsListener(view, null)
}
onDispose { onDestory() }
}
}
在你的 Composable 中使用 BottomNavBarVisibilityListener:
@Composable
fun MyScreen() {
var isBottomNavBarVisible by remember { mutableStateOf(true) }
BottomNavBarVisibilityListener { isVisible ->
isBottomNavBarVisible = isVisible
}
// 在这里根据isBottomNavBarVisible的值显示/隐藏底部导航栏
}
通过这种方式,你可以监听底部导航栏的显示与隐藏,并在 Composable 中相应地处理逻辑。
评论
扫描二维码获取文章详情
更多精彩内容尽在:WWW.ZNGG.NET