android - GridLayoutManager 跨度大小 RecycleView

标签 android view android-recyclerview gridlayoutmanager

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

有人可以帮忙吗?

更新

enter image description here

最佳答案

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

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

关于android - GridLayoutManager 跨度大小 RecycleView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31511700/

相关文章:

java.lang.ClassCastException : android. widget.LinearLayout$LayoutParams 无法转换为 android.widget.AbsListView$LayoutParams

android - 如何使用 Android 1.6 中加速度计的 x、y、z 值计算重力

android - OkHttp3 缓存似乎未通过 Retrofit 2 检查

android - 如何从 Fragment 访问 RecyclerView 中的数据?

android - 将 gridview 替换为 recyclerview

java - 带有 RecyclerView 的自定义 AlertDialog - 列表始终为空

android - 为什么 native 线程在应用程序处于后台时表现不同?

MySQL - 从 View 中选择或直接选择

css - 仅将 View 的 css 代码直接放在 View 中

asp.net-mvc - 将缓存对象公开给 View 的最佳方法