You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让RecyclerView仅显示2个可滚动的列表项?

RecyclerView仅显示2个可滚动元素的实现问题

我通过ListAdapter在RecyclerView中展示一组操作列表,希望RecyclerView仅显示2个元素且支持滚动,但目前无法实现该效果。相关代码如下:

ListAdapter代码

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) {
        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
            }
        }
    }
}

协程获取Room数据代码

GlobalScope.launch(Dispatchers.IO){
    sharedViewModel.getOperationsAll(ACCOUNT.number).collect(){ it ->
        operationAdapter.submitList(it.sortedByDescending { it.time })
    }
}
解决方法
  • 给RecyclerView设置固定高度:如果RecyclerView的高度是wrap_content,它会自动适配所有列表项的高度,导致显示全部元素。可以设置一个刚好能容纳2个列表项的固定高度,比如:
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/operationRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="120dp" <!-- 根据你的列表项高度调整此值 -->
        android:scrollbars="vertical" />
    
  • 确认布局管理器配置:确保RecyclerView使用的是垂直方向的LinearLayoutManager,默认配置即可,不需要额外限制数量,只要RecyclerView高度足够小,就会自动支持滚动:
    val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
    operationRecyclerView.layoutManager = layoutManager
    
  • 检查列表项布局高度:如果列表项的布局高度设置不合理(比如过高),会导致RecyclerView无法显示2个完整项。确保列表项高度是固定值或合适的wrap_content,保证2个项能在设置的RecyclerView高度内完整显示。
  • 限制RecyclerView父布局高度:如果RecyclerView的父布局是match_parent且没有高度限制,RecyclerView会占满父布局空间,显示更多元素。要确保父布局的高度约束能让RecyclerView只显示2个项。

内容的提问来源于stack exchange,提问作者Artem Barabash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 10:50:28