tim*_*m p 1 android kotlin android-jetpack-compose
所以我尝试遵循 jetpack compose forilarows 的示例
创建惰性行对象时,除非我指定此导入: import androidx.compose.foundation.lazy.items
lazyrow 函数内的 items 函数抛出此错误:类型不匹配:推断类型为 List<DrawableStringPair> 但需要 Int
我相信它会根据输入使用不同的函数,但我试图找出为什么上面的 import 语句修复了它。所有“items”函数都在同一文件“LazyDsl.kt”中定义
我想我的问题是,上面的导入如何指定其此功能:
inline fun <T> LazyListScope.items(
items: List<T>,
noinline key: ((item: T) -> Any)? = null,
noinline contentType: (item: T) -> Any? = { null },
crossinline itemContent: @Composable LazyItemScope.(item: T) -> Unit
) = items(
Run Code Online (Sandbox Code Playgroud)
而不是这个:
fun items(
count: Int,
key: ((index: Int) -> Any)? = null,
contentType: (index: Int) -> Any? = { null },
itemContent: @Composable LazyItemScope.(index: Int) -> Unit
) {
error("The method is not implemented")
}
Run Code Online (Sandbox Code Playgroud)
数据:
private val alignYourBodyData = listOf(
R.drawable.ab1_inversions to R.string.ab1_inversions,
R.drawable.ab2_quick_yoga to R.string.ab2_quick_yoga,
R.drawable.ab3_stretching to R.string.ab3_stretching,
R.drawable.ab4_tabata to R.string.ab4_tabata,
R.drawable.ab5_hiit to R.string.ab5_hiit,
R.drawable.ab6_pre_natal_yoga to R.string.ab6_pre_natal_yoga
).map { DrawableStringPair(it.first, it.second) }
Run Code Online (Sandbox Code Playgroud)
导致问题的函数:
@Composable
fun AlignYourBodyRow(
modifier: Modifier = Modifier
) {
LazyRow(
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier = modifier
) {
items(alignYourBodyData) { item ->
AlignYourBodyElement(item.drawable, item.text)
}
}
}
Run Code Online (Sandbox Code Playgroud)
可组合函数:
@Composable
fun AlignYourBodyElement(
@DrawableRes drawable: Int,
@StringRes text: Int,
modifier: Modifier = Modifier
) {
Column(
modifier = modifier,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
painter = painterResource(drawable),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier
.size(88.dp)
.clip(CircleShape)
)
Text(
text = stringResource(text),
style = MaterialTheme.typography.h3,
modifier = Modifier.paddingFromBaseline(
top = 24.dp, bottom = 8.dp
)
)
}
}
Run Code Online (Sandbox Code Playgroud)
修复一切的导入:
import androidx.compose.foundation.lazy.items
归档时间: |
|
查看次数: |
657 次 |
最近记录: |