Last active
March 11, 2024 08:26
-
-
Save janheinrichmerker/2fcb22f160eefee6f07b to your computer and use it in GitHub Desktop.
GridLayoutManager implementation that stretches to fit all grid items on screen and disables scrolling. Useful for dashboards etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First of all, thank you for this snippet. It has been of much help to me.
But I had a problem: as every item in the recyclerview has to appear on the screen, every time a change happens such as items are added or removed programmatically, all items has to be resized. Generally it works correctly but sometimes not, just only changing the item in question.
Also I had trouble when trying to change programatically the spanCount using setSpanCount.
Therefore, I made some changes to your code. I apologize I don't know how to make a new revision of this gist, and I have created a fork to upload my changes (maybe this is the right way to make a revision), what include:
You can find it at https://gist.github.com/vyguera/e6babe1995bff6e8694c2a14b69c595c