How to add dividers and spaces between items in RecyclerView?

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

    Too Late but for GridLayoutManager I use this:

    public class GridSpacesItemDecoration : RecyclerView.ItemDecoration
    {
        private int space;
    
        public GridSpacesItemDecoration(int space) {
            this.space = space;
        }
    
        public override void GetItemOffsets(Android.Graphics.Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
        {
            var position = parent.GetChildLayoutPosition(view);
    
            /// Only for GridLayoutManager Layouts
            var manager = parent.GetLayoutManager() as GridLayoutManager;
    
            if (parent.GetChildLayoutPosition(view) < manager.SpanCount)
                outRect.Top = space;
    
            if (position % 2 != 0) {
                outRect.Right = space;
            }
    
            outRect.Left = space;
            outRect.Bottom = space;
        }
    }
    

    This work for any span count you have.

    Ollie.

提交回复
热议问题