Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GridLayoutManager with working wrap_content.
package com.inperson.android.utils.leastview;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
public class WrappableGridLayoutManager extends GridLayoutManager {
public WrappableGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
private int[] mMeasuredDimension = new int[2];
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
for (int i = 0; i < getItemCount(); i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
if (i % getSpanCount() == 0) {
width = width + mMeasuredDimension[0];
}
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
if (i % getSpanCount() == 0) {
height = height + mMeasuredDimension[1];
}
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) {
View view = recycler.getViewForPosition(position);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
}
}
@riyazMuhammad

This comment has been minimized.

Copy link

riyazMuhammad commented Sep 24, 2015

Good one buddy.

I tried it with the items being a textview and compound drawables on it. So I was seeing a little scroll and then top most and bottom most rows were being cut a little. so did the following

  1. setMeasuredDimension(width, height+10);
@Override
public boolean canScrollVertically() {
   return false;
}

And now it's all fine. Hope it helps someone.

@NileshJarad

This comment has been minimized.

Copy link

NileshJarad commented Oct 9, 2015

I used this to Wrap the Grid of RecyclerView Working fine

mMultiSelectView.setLayoutManager(new WrappableGridLayoutManager(mContext, 2));

But problem is when I used SpaceItemDecoration its cutting the item
mMultiSelectView.addItemDecoration(new SpaceItemDecoration(20, LinearLayoutManager.VERTICAL));

Help me with this, Thanks.

@gzncland

This comment has been minimized.

Copy link

gzncland commented Oct 13, 2015

Great Thanks

@zeinabmohamed

This comment has been minimized.

Copy link

zeinabmohamed commented Oct 26, 2015

@riyazMuhammad please could u describe where exctly set

  1. setMeasuredDimension(width, height+10);
    as i set in onMesure but no effect
@ashton719

This comment has been minimized.

Copy link

ashton719 commented Nov 16, 2015

if child layout has that Custom Square Image View

List size = measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(widthSize / getSpanCount(), widthMode),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED));

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 28, 2015

Hi
What is the license of your code ?

@kyonghwa

This comment has been minimized.

Copy link

kyonghwa commented Jan 13, 2016

Works perfectly
Thanks

@harshbangari

This comment has been minimized.

Copy link

harshbangari commented Mar 10, 2016

//Method threw 'java.lang.IndexOutOfBoundsException' :Invalid item position 0(0). Item count:0 in measureScrapChild() at line:
View view = recycler.getViewForPosition(position);

@ccpy

This comment has been minimized.

Copy link

ccpy commented Apr 28, 2016

//Method threw 'java.lang.IndexOutOfBoundsException' :Invalid item position 0(0). Item count:0 in measureScrapChild() at line:
View view = recycler.getViewForPosition(position);

@geolyth

This comment has been minimized.

Copy link

geolyth commented Apr 28, 2016

With Android M got this:

//Method threw 'java.lang.IndexOutOfBoundsException' :Invalid item position 0(0). Item count:0 in measureScrapChild() at line:
View view = recycler.getViewForPosition(position);

@zoomheng

This comment has been minimized.

Copy link

zoomheng commented Jun 28, 2016

//Method threw 'java.lang.IndexOutOfBoundsException' :Invalid item position 0(0). Item count:0 in measureScrapChild() at line:
View view = recycler.getViewForPosition(position);

@Dmitry-Borodin

This comment has been minimized.

Copy link

Dmitry-Borodin commented Aug 18, 2016

to fix

:Invalid item position 0(0). Item count:0 in measureScrapChild()

change View view = recycler.getViewForPosition(position); to this code:

View view = null;
        try {
            view = recycler.getViewForPosition(position);
        } catch (Exception ex) {
          // try - catch is needed since support library version 24
        }

@sough

This comment has been minimized.

Copy link

sough commented Jan 5, 2017

Hello, thx for this snippet, but i have a problem - recycler view binding ALL his views on initialisation and again on scroll

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.