首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在RecyclerView中只显示2项?

如何在RecyclerView中只显示2项?
EN

Stack Overflow用户
提问于 2022-10-25 17:31:31
回答 1查看 31关注 0票数 0

有一个操作列表,我通过ListAdapter在回收视图中显示。RecyclerView的大小是两个元素。

ListAdapter:

代码语言:javascript
复制
class OperationAdapter(private val onItemClicked: (Operation) -> Unit) :
ListAdapter<Operation, OperationAdapter.OperationViewHolder>(DiffCallback) {



override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OperationViewHolder {
    val viewHolder = OperationViewHolder(
        OperationItemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

    return viewHolder
}

@SuppressLint("ResourceAsColor", "SetTextI18n")
override fun onBindViewHolder(holder: OperationViewHolder, position: Int) {

    holder.bind(getItem(position))
}


class OperationViewHolder(private var binding: OperationItemBinding): RecyclerView.ViewHolder(binding.root) {
    /*val imageOperation:ImageView = view.findViewById(R.id.imageViewItem)
    val nameOperation:TextView = view.findViewById(R.id.name_operation)
    val balanceOperation:TextView = view.findViewById(R.id.textSum)*/
    fun bind(operation: Operation){
        if(operation.receive == ACCOUNT.number){
            binding.imageViewItem.setImageResource(R.drawable.ic_type_recieve)

            binding.nameOperation.text = operation.send
            binding.nameOperation.setTextColor(Color.rgb(35, 135, 0))

            val sum = NumberFormat.getCurrencyInstance(Locale("en", "US")).format(operation.sum)
            binding.textSum.text = sum
            binding.textSum.setTextColor(Color.rgb(35, 135, 0))
        }else{
            binding.imageViewItem.setImageResource(R.drawable.ic_type_sent)

            binding.nameOperation.text = operation.receive
            binding.nameOperation.setTextColor(Color.rgb(231, 223,255))

            val sum = NumberFormat.getCurrencyInstance(Locale("en", "US")).format(operation.sum)
            binding.textSum.text = "-$sum"
            binding.textSum.setTextColor(Color.rgb(231, 223,255))
        }
    }
}

companion object{
    private val DiffCallback = object: DiffUtil.ItemCallback<Operation>(){
        override fun areItemsTheSame(oldItem: Operation, newItem: Operation): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Operation, newItem: Operation): Boolean {
            return oldItem == newItem
        }

    }
}

}

我从房间里从沙发检索到了清单:

代码语言:javascript
复制
  GlobalScope.launch(Dispatchers.IO){
        sharedViewModel.getOperationsAll(ACCOUNT.number).collect(){ it ->
            operationAdapter.submitList(it.sortedByDescending { it.time })
        }
    }

我不能输出只有两个项目的回收视图,用户可以滚动它。

EN

回答 1

Stack Overflow用户

发布于 2022-10-25 17:58:39

您不能为此使用GlobalScope,因为它会在应用程序的整个生命周期内泄漏您的活动和/或片段。每次屏幕变化,它都会泄漏另一个拷贝,直到你的应用程序耗尽内存。

Dispatchers.IO是不必要的。您不能在这个协同线中调用任何阻塞函数。

若要限制为两项,可以在将列表传递给适配器之前在列表上使用take(2)

代码语言:javascript
复制
lifecycleScope.launch {
    sharedViewModel.getOperationsAll(ACCOUNT.number).collect { opList ->
        operationAdapter.submitList(opList.sortedByDescending { it.time }.take(2))
    }
}

如果这是一个片段,则应该使用viewLifecycleOwner.lifecycleScope

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74198038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档