GridLayoutManager Span Size RecycleView

2020-03-05 02:37发布

enter image description here I'm trying to achieve a layout similar to the picture above with the RecyclerView in conjunction with the GridLayoutManager, i tried setting the setSpanSizeLookup based on position but couldn't mimic the design above..

anyone could help please?

UPDATE

enter image description here

1条回答
倾城 Initia
2楼-- · 2020-03-05 03:20
 private GridLayoutManager getGridLayoutManager() {
    final GridLayoutManager manager = new GridLayoutManager(getActivity(), 6);
    manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int index = postion % 5;
            switch(index){
               case 0: return 2;
               case 1: return 2;
               case 2: return 2;
               case 3: return 3;
               case 4: return 3;
            }
           }
    });
    return manager;
}

Update for Margin

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
  private int space;

  public SpacesItemDecoration(int space) {
       this.space = space;
   }

   @Override
  public void getItemOffsets(Rect outRect, View view, 
  RecyclerView parent, RecyclerView.State state) {
    outRect.right = space;
    outRect.bottom = space;
  }
 }
查看更多
登录 后发表回答