因此,在我的应用程序中,我正在使用 Compose Paging,但遇到了一个问题,我不确定如何正确处理。我来描述一下这个问题:
在我的分页列表中,我显示两种类型的项目:标题和实际数据单元格。我从 API 获取这些数据单元格的数据,并使用 insertSeparators 方法在它们之间插入标题。
我通过使用密封类来区分标题和单元格数据对象:
sealed class PagingModel {
data class Header(@StringRes val label: Int) : PagingModel()
data class CellData(val data: DataResponse) : PagingModel()
}
Run Code Online (Sandbox Code Playgroud)
因此,在分页源中,我将 api 结果映射到PagingModel.CellDatainsertSeparators 中,并在 insertSeparators 中添加PagingModel.Header一些之前/之后的逻辑。
现在,说到问题。我希望分页列表尽可能高效,因此我想在 LazyColumn 中使用 key 和 contentType 参数。这是一个代码片段:
items(
count = lazyPagingItems.itemCount,
key = lazyPagingItems.itemKey { **What do I put here when PagingData.Header has no key** },
contentType = lazyPagingItems.itemContentType {**should it just be "it" ?** }
) { index -> ...} …Run Code Online (Sandbox Code Playgroud) android android-paging android-jetpack-compose android-paging-3
我想要一个执行此操作的数据类:
data class VariantModal (
val variantId: String,
val variantName: String,
val variantUnit: List<String> || variantUnitOptions
val productId: String,
val variantQuantity: Int
)
Run Code Online (Sandbox Code Playgroud)
variantUnit 可以是字符串列表或枚举值,
enum class variantUnitOptions {
KG,
BUNDLE,
BOX,
PIECE
}
Run Code Online (Sandbox Code Playgroud)
由于我们不能像 || 一样使用 OR,所以我尝试使用密封类以这种方式包含字符串或枚举列表,
sealed class VariantUnit<T>(val value: T) {
class ListOfUnits(value: List<String>) : VariantUnit< List<String>>(value)
class SelectedUnit(value: variantUnitOptions) : VariantUnit<variantUnitOptions (value)
}
data class VariantModal (
val variantId: String,
val variantName: String,
val variantUnit: VariantUnit<>, //error here
val productId: String,
val variantQuantity: Int
)
Run Code Online (Sandbox Code Playgroud)
但通过这种方式,我收到了 …
dialdValue 的值永远不会更新。它总是具有值“A” 如何正确链接 mutableStates?
@Composable
fun DropDownDemo() {
var expanded by remember { mutableStateOf(false) }
val items = listOf("A", "B", "C", "d", "E", "F")
var selectedIndex by remember { mutableStateOf(0) }
var disabledValue by remember { mutableStateOf(items[selectedIndex]) }
Log.e("mcheck", "$selectedIndex $disabledValue")
Box(
modifier = Modifier
.fillMaxSize()
.wrapContentSize(Alignment.TopStart)
)
{
Text(
items[selectedIndex],
modifier = Modifier
.fillMaxWidth()
.clickable(onClick = { expanded = true })
.background(Color.Gray)
)
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier
.fillMaxWidth()
.background(Color.Red) …Run Code Online (Sandbox Code Playgroud) 所以我尝试遵循 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 …Run Code Online (Sandbox Code Playgroud)