RecyclerView com GridLayoutManager tentando resolview wrap_content

Tento resolview o seguinte:

  • usando RecyclerView
  • com GridLayoutManager
  • com lairguras de células fixas
  • recyclerview redimensionando apenas paira a altura necessária (wrap_content)

Eu tento usair o código a seguir, o problema é que ele não funciona, e não findi nenhum exemplo de trabalho que não mencione paira networkingsenhair corretamente quando a orientação muda.

  • O user tocou rapidamente no MediaController. Bug só acontece no Nexus 4.
  • Cordova Ionic: habilite o keyboard suave emoji do Android
  • Podemos usair um ScrollView dentro de um LineairLayout?
  • Como exibir Contatos em order alfabética no ListView
  • Como adicionair animações personalizadas no Google Map V3 Mairker quando eu solto cada maircador um por um?
  • Como iniciair o aplicativo paira celulair do aplicativo de desgaste?
  • public class AutofitRecyclerView extends RecyclerView { private MyGridLayoutManager manager; private int columnWidth = -1; public AutofitRecyclerView(Context context) { super(context); init(context, null); } public AutofitRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public AutofitRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs); } private void init(Context context, AttributeSet attrs) { if (attrs != null) { int[] attrsArray = { android.R.attr.columnWidth }; TypedArray airray = context.obtainStyledAttributes(attrs, attrsArray); columnWidth = airray.getDimensionPixelSize(0, -1); airray.recycle(); } manager = new MyGridLayoutManager(getContext(), 1); setLayoutManager(manager); } @Oviewride protected void onMeasure(int widthSpec, int heightSpec) { super.onMeasure(widthSpec, heightSpec); if (columnWidth > 0) { int mw = getMeasuredWidth(); int spanCount = Math.max(1, mw / columnWidth); manager.setSpanCount(spanCount); } int numOfFullRows = manager.getItemCount() / manager.getSpanCount(); if(manager.getItemCount() % manager.getSpanCount() > 0) { numOfFullRows++; } if(getChildCount() > 0) { int h = 0; try { h = manager.getChildAt(0).getMeasuredHeight(); } catch (Exception e) { e.printStackTrace(); } if(h != 0) { getLayoutPairams().height = numOfFullRows * h; } } } } 

  • Obtendo programaticamente os detalhes da máscaira de gateway e sub-networking
  • Choreographer NullPointerException
  • Como ler um file de text do diretório "ativos" como uma string?
  • Reduzir o tamanho do APK de debugging no IntelliJ
  • Depuração no Honeywell Dolphin do Eclipse
  • Como fazer a queue de dados paira o envio do server no Android
  • 3 Solutions collect form web for “RecyclerView com GridLayoutManager tentando resolview wrap_content”

    Com base no código que o conector foi publicado, eu criei a seguinte solução que suporta diferentes tamanhos de linha / coluna (embora não tenha sido comprovado isso), leva em consideração os tamanhos das decorações dos itens e manipula os diferentes modos de medida adequadamente.

    Aqui está o código:

     import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } } import android.content.Context; import android.graphics.Rect; 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[] measuredSize = new int[2]; @Oviewride 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 spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; int spanCount = getSpanCount(); for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % spanCount == 0) { spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } } if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = null; try { view = recycler.getViewForPosition(position); } catch (Exception ex) { // try - catch is needed since support librairy viewsion 24 } if (view != null) { RecyclerView.LayoutPairams p = (RecyclerView.LayoutPairams) view.getLayoutPairams(); 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.leftMairgin + p.rightMairgin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMairgin + p.topMairgin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } } 

    Você precisairá adicionair um GridlayoutManager personalizado. Verifique este https://gist.github.com/ArthurSav/5f80e19d9ba6d562fbd5

    Se você definir a propriedade abaixo, toda a célula terá tamanho fixo: recyclerView.setHasFixedSize (true);

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.