Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save epool/7cd41cb1e3bc3f18d4d3165ee6821059 to your computer and use it in GitHub Desktop.
Save epool/7cd41cb1e3bc3f18d4d3165ee6821059 to your computer and use it in GitHub Desktop.

Revisions

  1. @janheinrichmerker janheinrichmerker revised this gist Jul 14, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions SpanningGridLayoutManager.java
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@
    import android.view.ViewGroup;

    public class SpanningGridLayoutManager extends GridLayoutManager {

    public SpanningGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    }
  2. @janheinrichmerker janheinrichmerker created this gist Jul 14, 2015.
    68 changes: 68 additions & 0 deletions SpanningGridLayoutManager.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    package com.example;

    import android.content.Context;
    import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.AttributeSet;
    import android.view.ViewGroup;

    public class SpanningGridLayoutManager extends GridLayoutManager {
    public SpanningGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    }

    public SpanningGridLayoutManager(Context context, int spanCount) {
    super(context, spanCount);
    }

    public SpanningGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
    super(context, spanCount, orientation, reverseLayout);
    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
    return spanLayoutSize(super.generateDefaultLayoutParams());
    }

    @Override
    public RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
    return spanLayoutSize(super.generateLayoutParams(c, attrs));
    }

    @Override
    public RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
    return spanLayoutSize(super.generateLayoutParams(lp));
    }

    @Override
    public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
    return super.checkLayoutParams(lp);
    }

    private RecyclerView.LayoutParams spanLayoutSize(RecyclerView.LayoutParams layoutParams){
    if(getOrientation() == HORIZONTAL){
    layoutParams.width = (int) Math.round(getHorizontalSpace() / Math.ceil(getItemCount() / getSpanCount()));
    }
    else if(getOrientation() == VERTICAL){
    layoutParams.height = (int) Math.round(getVerticalSpace() / Math.ceil(getItemCount() / getSpanCount()));
    }
    return layoutParams;
    }

    @Override
    public boolean canScrollVertically() {
    return false;
    }
    @Override
    public boolean canScrollHorizontally() {
    return false;
    }

    private int getHorizontalSpace() {
    return getWidth() - getPaddingRight() - getPaddingLeft();
    }

    private int getVerticalSpace() {
    return getHeight() - getPaddingBottom() - getPaddingTop();
    }
    }