gaohomway
gaohomway

Reputation: 4060

Jetpack Compose general item in LazyColumn

In the following code, I have two parts A and B. I need to extract part B as a common part for more of my pages.
But it contains item, I cannot extract item, but item must be included in the if judgment because paging3 will scroll to the top for extra item.

Is there a way to extract item?

LazyColumn(Modifier.fillMaxSize()) {
    // Part A
    items(pagingItems) { wind ->
        WindRow(navController, wind!!)
    }

    val refresh = pagingItems.loadState.refresh
    val append = pagingItems.loadState.append

    // Part B
    if (refresh is LoadState.NotLoading && append is LoadState.NotLoading) {
        if (pagingItems.itemCount == 0) {
            item {
                PosterCompose() {
                    navController.navigate("blowWind")
                }
            }
        }
    } else {
        item {
            LoadStateView(path = FOLLOW_WIND_LIST, refresh = refresh, append = append) {
                pagingItems.retry()
            }
        }
    }
}

Upvotes: 1

Views: 1055

Answers (1)

gaohomway
gaohomway

Reputation: 4060

I solved this problem

fun <T : Any> LazyListScope.newItems(pagingItems: LazyPagingItems<T>) {

    val refresh = pagingItems.loadState.refresh
    val append = pagingItems.loadState.append

    if (refresh is LoadState.NotLoading && append is LoadState.NotLoading) {
        if (pagingItems.itemCount == 0) {
            item {
                PosterCompose() {

                }
            }
        }
    }else{
        item {
            LoadStateView(path = FOLLOW_WIND_LIST, refresh = refresh, append = append) {
                pagingItems.retry()
            }
        }
    }

}

This is how to use

LazyColumn(Modifier.fillMaxSize()) {

            items(pagingItems) { wind ->
                WindRow(navController, wind!!)
            }

            newItems(pagingItems)
        }

Upvotes: 1

Related Questions