我目前在 Jetpack Compose 中遇到一个非常奇怪的问题。我正在使用导航组件库(在 NavHostFragment 中托管片段)。一些较新的 Fragment 已经在使用 Compose,但我在极少数情况下会遇到此崩溃。主要是在使用导航抽屉导航到设置页面并单击底部导航视图中的某个项目时。我目前正在使用 Jetpack Compose alpha-12 和 Navigation 2.3.0。
(使用普通 ViewBinding 时不会崩溃)
@AndroidEntryPoint
class StatisticsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setContent {
TemporyTheme {
Text("Hello Compose!")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
...>
<RelativeLayout
...>
<RelativeLayout
...>
<com.google.android.material.appbar.MaterialToolbar
... />
</RelativeLayout>
<!-- NavHost -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_main"
... />
<com.google.android.material.bottomnavigation.BottomNavigationView
... />
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
.../> …Run Code Online (Sandbox Code Playgroud) 致力于增强我的应用程序的辅助功能。
从Docs中,我可以看到Card和onClick作为onClickLabelAPI 的一部分。
对于没有 的可组合项onClick,我们可以使用Modifier.clickableor Modifier.semantics。
但是Button、IconButton、FloatingActionButton和TextButton呢OutlinedButton?
可组合项onClick作为 API 的一部分,但没有onClickLabel.
这按预期工作。但这是编写这段代码的正确方法吗?
IconButton(
onClick = navigateUp,
modifier = Modifier.clickable(
onClickLabel = "Navigate up",
onClick = {},
)
) {
Icon(
imageVector = Icons.Rounded.ArrowBack,
contentDescription = stringResource(
id = R.string.top_app_bar_content_description_navigate_up,
),
)
}
Run Code Online (Sandbox Code Playgroud)
这似乎太令人困惑IconButton了onClick。所以我不能完全同意Modifier.clickable或Modifier.semantics。
同样Modifier.clickable也是期待 …
我正在使用 jetpack compose 库为 Android 设备开发一个应用程序。
我在 compose 函数内创建的示例
var taskVeriable = 记住 {mutableStateOf("Hello World")}
我需要从另一个组合函数更新变量的值。有什么办法可以实现这一点吗?
@Composable
fun TestComposeA(){
var taskVeriable = remember {mutableStateOf("Hello World")}
TestComposeB(taskVeriable)
}
@Composable
fun TestComposeB(taskVeriable : String){
taskVeriable = "New Value"
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有办法做到这一点。
我想要实现与在 stackOverFlow 问题中添加标签相同的效果,我们为如何在 jetpack compose 中执行此操作的问题选择标签,如下所示,单击“x”图标应删除标签

我以这种方式开始,但很困惑从哪里开始以及如何实现添加带有 icon 的按钮的想法。任何人都可以指导我如何实现它
我尝试过,但预览后我很困惑我应该使用带有图标的按钮还是基本的文本字段......
@Composable
fun MultipleTextFieldWithDeleteIcon(
text: String,
hint: String,
modifier: Modifier = Modifier,
isHintVisible: Boolean = true,
onValueChange: (String) -> Unit,
textStyle: TextStyle = TextStyle()
) {
Box(
modifier = modifier
) {
BasicTextField(
value = text,
onValueChange = onValueChange,
textStyle = textStyle,
modifier = Modifier
.align(Alignment.Center)
.height(100.dp)
.width(300.dp)
)
if(isHintVisible) {
Text(text = hint, style = textStyle, color = Color.DarkGray)
}
}
}
@Preview
@Composable
fun AboveTextFieldWithIconPreview() {
MultipleTextFieldWithDeleteIcon(text = "android, …Run Code Online (Sandbox Code Playgroud) android kotlin android-jetpack-compose android-compose-textfield
我想我还没有完全理解组合状态是如何工作的。当 uiState 中的项目发生更改时,我无法触发重组。
我正在构建一个需要通知访问的应用程序,因此我将用户导航到设置,并且在用户授予权限后,他们必须导航回该应用程序。这就是我想触发重组的地方。
我在 onResume 工作中进行了权限检查,并且 uiState 中的变量发生了变化,但没有调用重组。我在这里缺少什么?
可组合的
@Composable
private fun MainLayout(viewModel: SetupViewModel){
val uiState = viewModel.uiState.collectAsState()
SetupItem(
title = "Notification access",
summary = if(uiState.value.hasNotificationPermission) stringResource(R.string.granted) else stringResource(R.string.not_granted){}
}
Run Code Online (Sandbox Code Playgroud)
设置UiState.kt
data class SetupUiState(
var hasNotificationPermission: Boolean = false
)
Run Code Online (Sandbox Code Playgroud)
我知道一个事实hasNotificationPermission被设置为 true,但 SetupItem 中的摘要不会更新。我该如何做到这一点?
我正在“项目”中添加我的笔记,但出现此错误。我找不到原因,你能帮忙吗?
@Composable
fun NotesList() {
val notesList = remember { mutableStateListOf<Note>() }
LazyColumn {
items(notesList) {note->
NoteCard(note.title, note.content,note.color,note.liked)
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我将光标悬停在 noteList 上时,出现以下错误。
类型不匹配。必需:Int 找到:SnapshotStateList
我希望通过将我的列表添加到项目中可以发挥作用,但事实并非如此。
Jetpack Compose animateScrollTo 到绝对坐标或直接到元素
这是几乎相同的问题,但我在我的情况下嵌套了可组合项,因此当您想从屏幕底部滚动时,接受的答案将不起作用。
relocationRequester 对我的情况没有帮助。
我尝试从坐标中获取parentCoordinates,但它看起来不像是一种工作方式。
以下是我滚动屏幕时日志的位置,目标可组合在屏幕中间的某个位置。
root > 3804 > parent > 231 > window > 3870
root > 3767 > parent > 231 > window > 3833
root > 3749 > parent > 231 > window > 3815
root > 3725 > parent > 231 > window > 3791
root > 3710 > parent > 231 > window > 3776
root > 3701 > parent > 231 > window > 3767
root > 3692 > parent …Run Code Online (Sandbox Code Playgroud) 设备的网络正常工作,但如屏幕截图所示,我无法连接到 URL: maven.google.com,导致撰写检查器无法正常工作。
我尝试修改旧版本 Compose 的版本,但没有解决问题。
我可以以某种方式设置 URL 或其他解决方案,以便我可以使用 compose 检查器吗?