How to add dividers and spaces between items in RecyclerView?

前端 未结 30 3355
清歌不尽
清歌不尽 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:34

    If anyone is looking to only add, say, 10dp spacing between items, you can do so by setting a drawable to DividerItemDecoration:

    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
        recyclerView.getContext(),
        layoutManager.getOrientation()
    );
    
    dividerItemDecoration.setDrawable(
        ContextCompat.getDrawable(getContext(), R.drawable.divider_10dp)
    ); 
    

    Where divider_10dpis a drawable resource containing:

    
        
        
    
    

提交回复
热议问题