GridLayoutManager Span Size RecycleView

时间秒杀一切 提交于 2020-01-11 03:34:07

问题


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


回答1:


 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;
  }
 }


来源:https://stackoverflow.com/questions/31511700/gridlayoutmanager-span-size-recycleview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!