コード例 #1
0
 public HeaderItemDecoration(RecyclerView.LayoutManager layoutManager, int height) {
   if (layoutManager.getClass() == LinearLayoutManager.class) {
     mNumberOfChildren = 1;
   } else if (layoutManager.getClass() == GridLayoutManager.class) {
     mNumberOfChildren = ((GridLayoutManager) layoutManager).getSpanCount();
   } else if (layoutManager instanceof StaggeredGridLayoutManager) {
     mNumberOfChildren = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
   }
   mHeaderHeight = height;
 }
コード例 #2
0
  private void validateRecycler(RecyclerView recycler, boolean headerAlreadyAligned) {
    RecyclerView.LayoutManager layoutManager = recycler.getLayoutManager();
    if (layoutManager == null) {
      throw new IllegalStateException(
          "Be sure to call RecyclerViewHeader constructor after setting your RecyclerView's LayoutManager.");
    } else if (layoutManager.getClass()
            != LinearLayoutManager.class // not using instanceof on purpose
        && layoutManager.getClass() != GridLayoutManager.class
        && !(layoutManager instanceof StaggeredGridLayoutManager)) {
      throw new IllegalArgumentException(
          "Currently RecyclerViewHeader supports only LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager.");
    }

    if (layoutManager instanceof LinearLayoutManager) {
      if (((LinearLayoutManager) layoutManager).getOrientation() != LinearLayoutManager.VERTICAL) {
        throw new IllegalArgumentException(
            "Currently RecyclerViewHeader supports only VERTICAL orientation LayoutManagers.");
      }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      if (((StaggeredGridLayoutManager) layoutManager).getOrientation()
          != StaggeredGridLayoutManager.VERTICAL) {
        throw new IllegalArgumentException(
            "Currently RecyclerViewHeader supports only VERTICAL orientation StaggeredGridLayoutManagers.");
      }
    }

    if (!headerAlreadyAligned) {
      ViewParent parent = recycler.getParent();
      if (parent != null
          && !(parent instanceof LinearLayout)
          && !(parent instanceof FrameLayout)
          && !(parent instanceof RelativeLayout)) {
        throw new IllegalStateException(
            "Currently, NOT already aligned RecyclerViewHeader "
                + "can only be used for RecyclerView with a parent of one of types: LinearLayout, FrameLayout, RelativeLayout.");
      }
    }
  }