-
-
Save janheinrichmerker/2fcb22f160eefee6f07b to your computer and use it in GitHub Desktop.
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(); | |
} | |
} |
How to use it with the recyclerview ?
Thanks very much!
not working below 5 Os devices
I want totally opposite of this i want to enable scrolling after adding n number of grid items . I want to enable horizontal and vertical scrolling simultaneously. Is this possible using recycler-view grid layout manager ?
@rhlmshr use RecyclerView.setLayoutManager(...)
@brachu21 @SubhashPrajapati If you're having issues, would you mind sharing a stack trace and/or screenshot?
@brachu21 @SubhashPrajapati If you're having issues, would you mind sharing a stack trace and/or screenshot?
Hi @heinrichreimer,
I think I'm also experiencing the issue of @brachu21. This is my case:
The first 3 columns containing 16 data each, displays exactly what I need but when it reached the last column with only 9 data, it only displays 8 and the last data was left as shown in the screenshot above.
spanLayoutSize
should take into account any margins in the layoutParams
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:
- Changed from java to kotlin
- Override of checkLayoutParams method to invalidate all items sizes when a new item is added/removed.
You can find it at https://gist.github.com/vyguera/e6babe1995bff6e8694c2a14b69c595c
thanks
I got problem with displaying a gridview, It's not displaying for example 7 element list in grid view, but only 6. How can I change it