code: public static int getSpanCount(RecyclerView view) {
          LayoutManager layout = view.getLayoutManager();
          if (layout != null) {
              if (layout instanceof GridLayoutManager) {
                  return ((GridLayoutManager) layout).getSpanCount();
              } else if (layout instanceof StaggeredGridLayoutManager) {
                  return ((StaggeredGridLayoutManager) layout).getSpanCount();
              }
              return 1; // assuming LinearLayoutManager
          }
          return 0;
      }
docs: Get the number of spans laid out by the RecyclerView's LayoutManager.
  
  @return 0 if the RecyclerView doesn't have a LayoutManager