简体   繁体   English

如何仅在 Jetpack Compose 中没有返回任何项目时显示消息?

[英]How to display a message only when no items are returned in Jetpack Compose?

I'm trying to implement search feature with pagination.我正在尝试使用分页实现搜索功能。 This is what I have tried:这是我尝试过的:

fun SearchScreen(
    viewModel: SearchViewModel = hiltViewModel()
) {
    var search by rememberSaveable(
        stateSaver = TextFieldValue.Saver
    ) {
        mutableStateOf(TextFieldValue(""))
    }
    val searchText = search.text
    val searchItems = viewModel.getSearchItems(searchText).collectAsLazyPagingItems()

    Scaffold(
        topBar = {
            SearchTopBar(
                search = search
            )
        },
        content = {
            LazyVerticalGrid(
                columns = GridCells.Adaptive(minSize = 128.dp)
                content = {
                    items(searchItems.itemCount) { index ->
                        val item = searchItems[index]
                        ItemCard(item)
                    }
                }
            )
            if (searchItems.itemCount == 0 && searchText.isNotEmpty()) {
                Text("No items found.")
            }
        }
    )
}

Now when I perform a search that provides no results, I want to display a text.现在,当我执行没有结果的搜索时,我想显示一个文本。 Which works, but this text is displayed also while it is loading the results.哪个有效,但是在加载结果时也会显示此文本。 For example, I want to search for "bacon".例如,我想搜索“培根”。 I type b it loads the items starting with b , I type a , it shows the "No items found."我输入b它加载以b开头的项目,我输入a ,它显示“未找到项目”。 and then displays the items starting with ba .然后显示以ba开头的项目。 I want to display only when I have no results.我只想在没有结果时显示。 How can I do that?我怎样才能做到这一点?

Add if condition in your lazygrid content and add Text("No items found.") into item{} block.在您的惰性网格内容中添加 if 条件并将Text("No items found.")添加到item{}块中。

Your example;你的榜样;

@Composable
fun SearchScreen(
    viewModel: SearchViewModel = hiltViewModel()
) {
    var search by rememberSaveable(
        stateSaver = TextFieldValue.Saver
    ) {
        mutableStateOf(TextFieldValue(""))
    }
    val searchText = search.text
    val searchItems = viewModel.getSearchItems(searchText).collectAsLazyPagingItems()

    Scaffold(
        topBar = {
            SearchTopBar(
                search = search
            )
        },
        content = {
            LazyVerticalGrid(
                columns = GridCells.Adaptive(minSize = 128.dp)
                content = {
                    if(searchItems.isNotEmpty()){
                        items(searchItems.itemCount) { index ->
                           val item = searchItems[index]
                           ItemCard(item)
                        }
                    }else{
                        item {
                            Text("No items found.")
                        }
                    }
                }
            )
        }
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM