【问题标题】:Find and move specific item at the top of list在列表顶部查找并移动特定项目
【发布时间】:2021-11-20 07:48:17
【问题描述】:

我有字符串让我们假设“lorem”。

我从 Api 下载包含项目的列表,其中一个包含“lorem”作为字段(标题)。

如何在列表顶部移动该项目而不改变其他项目的位置?

 list = downloadedData.items?.map {
                        Item(
                            text = it.title,
                            onClick = Button { }
                        )
                    }

【问题讨论】:

    标签: android sorting kotlin


    【解决方案1】:

    对于mutable list: lorem 的第一次搜索 position 然后使用removeAt() 删除该项目。 然后在开头添加搜索项

    让 item 是您搜索的项目

    list.removeAt(position)
            list.add(0,item)
    

    【讨论】:

    • toMutableList().also { list -> list?.forEach { if(it.title == data.title){ val mainPosition = list.indexOf(it) list.removeAt(mainPosition) list .add(0, it) } } } 你知道我为什么会崩溃吗?
    • 什么是异常信息?
    • 主进程错误 RxJavaAssemblyException:assembled Edit: mainPosition was 0 I need to add if(mainDocumentPosition != 0)
    • 哦,对不起。这与那部分代码无关。
    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多