GridLayoutManager跨度大小RecycleView

时间:2015-07-20 08:16:58

标签: android view android-recyclerview gridlayoutmanager

enter image description here 我正在尝试使用RecyclerView和GridLayoutManager实现类似于上图的布局,我尝试根据位置设置setSpanSizeLookup但是无法模仿上面的设计..

有人可以帮忙吗?

更新

enter image description here

1 个答案:

答案 0 :(得分:10)

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

保证金更新

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