Kotlin随机图像在其他图像视图中无重复

发布于 2025-02-02 04:08:58 字数 738 浏览 5 评论 0原文

我该如何解决Kotlin,我即将完成一个应用程序,但我无法关闭所有内容?

我不擅长的功能

是,使用三个ipaimViews,我希望ImageView中的映像不要在ImageView 2和3中重复相同的数字,因此我希望列表成为一个,并且没有重复的内容可以显示这三个图像视图之间是代码:

 private lateinit var listId: List<Int>
 
 initializeList()
 
 pickNumber ()

    }

    private fun pickNumber (){
        binding.imageView.setImageResource(listId.random())
        binding.imageView2.setImageResource(listId.random())
        binding.imageView3.setImageResource(listId.random())
        
        
         private fun initializeList() {
        listId = listOf(
            R.drawable.we ,
            R.drawable.wq,
            R.drawable.set,
            R.drawable.opt,
            R.drawable.ups,
        )

带有80张图像

how can I solve in kotlin, I am about to finish an app but I can't close everything?

the function that I am not good at

the fact is that with three imageviews I would like the image in imageview not to repeat the same figure in imageview 2 and 3 so I would like the list to be one and that there are no duplicates to show between the three imageviews here is the code:

 private lateinit var listId: List<Int>
 
 initializeList()
 
 pickNumber ()

    }

    private fun pickNumber (){
        binding.imageView.setImageResource(listId.random())
        binding.imageView2.setImageResource(listId.random())
        binding.imageView3.setImageResource(listId.random())
        
        
         private fun initializeList() {
        listId = listOf(
            R.drawable.we ,
            R.drawable.wq,
            R.drawable.set,
            R.drawable.opt,
            R.drawable.ups,
        )

with 80 images

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

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

发布评论

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

评论(1

甜扑 2025-02-09 04:08:58

正如Gpunto建议的那样,您可以先洗个清单,然后逐一获取元素。

private fun pickNumber (){
    val (id1, id2, id3) = list.shuffled()  
    binding.imageView.setImageResource(id1)
    binding.imageView2.setImageResource(id2)
    binding.imageView3.setImageResource(id3)
}

在这里,我使用列表破坏以获取列表的第三个元素。

官方文档

As gpunto suggested, you can shuffle the list first and then fetch elements one by one.

private fun pickNumber (){
    val (id1, id2, id3) = list.shuffled()  
    binding.imageView.setImageResource(id1)
    binding.imageView2.setImageResource(id2)
    binding.imageView3.setImageResource(id3)
}

Here I used list destructuring to get first three elements of the list.

Official docs

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