Set Margin on RecyclerView programmatically

前端 未结 4 765
旧巷少年郎
旧巷少年郎 2021-01-12 00:13

I need to set the top margin on a RecyclerView programmatically, but I get this exception:

java.lang.RuntimeException: Unable to resume activity java.lang.Cla

4条回答
  •  借酒劲吻你
    2021-01-12 01:04

    for me, the parent of recyclerview is a constraintLayout,

            val margins = (rv.layoutParams as ConstraintLayout.LayoutParams).apply {
            leftMargin = 0
            rightMargin = 0
        }
        rv.layoutParams = margins
    

    or it will get cast error, viewGroup.LayoutParams can't be cast to COnstarintLayout.LayoutParams

提交回复
热议问题