How to add dividers and spaces between items in RecyclerView?

前端 未结 30 3563
清歌不尽
清歌不尽 2020-11-22 03:27

This is an example of how it could have been done previously in the ListView class, using the divider and dividerHeight parame

30条回答
  •  忘掉有多难
    2020-11-22 04:25

    Instead of create a shape xml for change the divider height and color. You can create programmatically like

    val divider = DividerItemDecoration(context,
            DividerItemDecoration.VERTICAL)
    
    divider.setDrawable(ShapeDrawable().apply {
        intrinsicHeight = resources.getDimensionPixelOffset(R.dimen.dp_15)
        paint.color = Color.RED // note: currently (support version 28.0.0), we can not use tranparent color here, if we use transparent, we still see a small divider line. So if we want to display transparent space, we can set color = background color or we can create a custom ItemDecoration instead of DividerItemDecoration. 
    })
    
    recycler_devices.addItemDecoration(divider)
    

提交回复
热议问题