如何在JetPack组成的LazyColumn中添加项目?

发布于 2025-02-08 21:42:00 字数 2306 浏览 1 评论 0原文

生成一个项目列表,

private val _balloonsStatus =
        MutableStateFlow<Status<List<BalloonsQuery.Edge>?>>(Status.Loading())
    val balloonsStatus get() = _balloonsStatus

private val _endCursor = MutableStateFlow<String?>(null)
    val endCursor get() = _endCursor

init {
        loadBalloons(null)
    }

fun loadBalloons(cursor: String?) {
        viewModelScope.launch {
            val data = repo.getBalloonsFromServer(cursor)
            if (data.errors == null) {
                _balloonsStatus.value = Status.Success(data.data?.balloons?.edges)
                _endCursor.value = data.data?.balloons?.pageInfo?.endCursor
            } else {
                _balloonsStatus.value = Status.Error(data.errors!![0].message)
                _endCursor.value = null
            }
        }
    }

在以下视图模型代码中,我正在从GraphQl Server和Composobable函数中

@Composable
fun BalloonsScreen(
    navHostController: NavHostController? = null,
    viewModel: SharedBalloonViewModel
) {

    val endCursor by viewModel.endCursor.collectAsState()
    val balloons by viewModel.balloonsStatus.collectAsState()
    AssignmentTheme {
        Column(modifier = Modifier.fillMaxSize()) {
            when (balloons) {
                is Status.Error -> {
                    Log.i("Reyjohn", balloons.message!!)
                }
                is Status.Loading -> {
                    Log.i("Reyjohn", "loading..")
                }
                is Status.Success -> {
                        BalloonList(edgeList = balloons.data!!, navHostController = navHostController)
                }
            }
            Spacer(modifier = Modifier.weight(1f))
            Button(onClick = { viewModel.loadBalloons(endCursor) }) {
                Text(text = "Load More")
            }
        }
    }

}

@Composable
fun BalloonList(
    edgeList: List<BalloonsQuery.Edge>,
    navHostController: NavHostController? = null,
) {
    LazyColumn {
        items(items = edgeList) { edge ->
            UserRow(edge.node, navHostController)
        }
    }
}

我通过遵循此代码来获取此数据:但是问题是每次我单击加载更多 tusta再生视图并显示一组新列表,但是我想在上一个列表末尾附加列表。据我了解,随着我正在听的流程的重新生成,这是在做这件事背后的工作,但是我坚持在这里进行有关如何在这里实现目标的解决方法,这将是一个善良的帮助!

In the following viewModel code I am generating a list of items from graphQl server

private val _balloonsStatus =
        MutableStateFlow<Status<List<BalloonsQuery.Edge>?>>(Status.Loading())
    val balloonsStatus get() = _balloonsStatus

private val _endCursor = MutableStateFlow<String?>(null)
    val endCursor get() = _endCursor

init {
        loadBalloons(null)
    }

fun loadBalloons(cursor: String?) {
        viewModelScope.launch {
            val data = repo.getBalloonsFromServer(cursor)
            if (data.errors == null) {
                _balloonsStatus.value = Status.Success(data.data?.balloons?.edges)
                _endCursor.value = data.data?.balloons?.pageInfo?.endCursor
            } else {
                _balloonsStatus.value = Status.Error(data.errors!![0].message)
                _endCursor.value = null
            }
        }
    }

and in the composable function I am getting this data by following this code:

@Composable
fun BalloonsScreen(
    navHostController: NavHostController? = null,
    viewModel: SharedBalloonViewModel
) {

    val endCursor by viewModel.endCursor.collectAsState()
    val balloons by viewModel.balloonsStatus.collectAsState()
    AssignmentTheme {
        Column(modifier = Modifier.fillMaxSize()) {
            when (balloons) {
                is Status.Error -> {
                    Log.i("Reyjohn", balloons.message!!)
                }
                is Status.Loading -> {
                    Log.i("Reyjohn", "loading..")
                }
                is Status.Success -> {
                        BalloonList(edgeList = balloons.data!!, navHostController = navHostController)
                }
            }
            Spacer(modifier = Modifier.weight(1f))
            Button(onClick = { viewModel.loadBalloons(endCursor) }) {
                Text(text = "Load More")
            }
        }
    }

}

@Composable
fun BalloonList(
    edgeList: List<BalloonsQuery.Edge>,
    navHostController: NavHostController? = null,
) {
    LazyColumn {
        items(items = edgeList) { edge ->
            UserRow(edge.node, navHostController)
        }
    }
}

but the problem is every time I click on Load More button it regenerates the view and displays a new set of list, but I want to append the list at the end of the previous list. As far I understand that the list is regenerated as the flow I am listening to is doing the work behind this, but I am stuck here to get a workaround about how to achieve my target here, a kind hearted help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2025-02-15 21:42:00

list&lt; balloonsquery.gedge&gt;?

该列表添加

_balloonsStatus.value = Status.Success(data.data?.balloons?.edges)

_balloonsStatus.value = Status.Success(myLiast.addAll(
data.data?.balloons?.edges))

可以在ViewModel中创建一个私有列表,

You can create a private list in ViewModel that adds List<BalloonsQuery.Edge>?>

and instead of

_balloonsStatus.value = Status.Success(data.data?.balloons?.edges)

you can do something like

_balloonsStatus.value = Status.Success(myLiast.addAll(
data.data?.balloons?.edges))

should update Compose with the latest data appended to existing one

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文