本文介绍了RecyclerView中最后一个孩子的边距/填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在最后一行添加填充/边距底部,在第一行添加填充/边距顶部.我无法在xml项目中执行此操作,因为它会影响我所有的孩子.
I'm trying to add Padding/Margin Bottom in the last row and Padding/Margin Top in the first row. I can not do it in the item xml as it would affect all of my Childs.
我的RecyclerView适配器中有头文件和子文件,因此我无法使用
I have headers and childs in my RecyclerView Adapter so I can not use the
android:padding="4dp"
android:clipToPadding="false"
我需要在每个标题的最后第一行单独使用它
I need to use it individually on the last first row of each header
推荐答案
我在Kotlin中使用它
I use this in kotlin
override fun onBindViewHolder(holder: RecyclerView.ViewHolder(view), position: Int) {
if (position == itemsList.lastIndex){
val params = holder.itemView.layoutParams as FrameLayout.LayoutParams
params.bottomMargin = 100
holder.itemView.layoutParams = params
}else{
val params = holder.itemView.layoutParams as RecyclerView.LayoutParams
params.bottomMargin = 0
holder.itemView.layoutParams = params
}
//other codes ...
}
这篇关于RecyclerView中最后一个孩子的边距/填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!