How to add dividers and spaces between items in RecyclerView?

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

    Just add

    recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),
                    DividerItemDecoration.VERTICAL));
    

    Also you may need to add the dependency
    compile 'com.android.support:recyclerview-v7:27.1.0'

    EDIT:

    For customizing it a little bit you can add a custom drawable:

    DividerItemDecoration itemDecorator = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
    itemDecorator.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.divider));
    

    You are free to use any custom drawable, for instance:

    
        
        
    
    

提交回复
热议问题