How to set the height of an item row in GridLayoutManager

后端 未结 8 628
有刺的猬
有刺的猬 2021-01-31 03:18

My Recycler Item which inflate in onCreateViewHolder




        
8条回答
  •  再見小時候
    2021-01-31 03:54

    kotlin version

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        binding.root.post {
            binding.root.layoutParams.height = parent.width/3
            binding.root.requestLayout()
        }
        return ViewHolder(binding)
    }
    

    here 3 is the span count of your GridLayoutManager . You can replace binding.root with your itemView , if you are not using Databinding

提交回复
热议问题