Created
August 18, 2016 07:51
-
-
Save Zhiw/212d4d9bb54b2bed2387158e026c8e01 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.kmhope.awst.view; | |
import android.content.Context; | |
import android.support.v7.widget.GridLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import android.view.ViewGroup; | |
/** | |
* ClassName: MyLayoutManager | |
* Desc: | |
* Created by zhiw on 16/8/18. | |
*/ | |
public class MyLayoutManager extends GridLayoutManager { | |
private int mChildPerLines; | |
private int[] mMeasuredDimension = new int[2]; | |
public MyLayoutManager(Context context, int spanCount) { | |
super(context, spanCount); | |
mChildPerLines = spanCount; | |
} | |
@Override | |
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { | |
final int heightMode = View.MeasureSpec.getMode(heightSpec); | |
final int widthSize = View.MeasureSpec.getSize(widthSpec); | |
final int heightSize = View.MeasureSpec.getSize(heightSpec); | |
int height = 0; | |
for (int i = 0; i < getItemCount(); ) { | |
measureScrapChild(recycler, i, | |
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), | |
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), | |
mMeasuredDimension); | |
height = height + mMeasuredDimension[1]; | |
i = i + mChildPerLines; | |
} | |
// If child view is more than screen size, there is no need to make it wrap content. We can use original onMeasure() so we can scroll view. | |
if (height < heightSize) { | |
switch (heightMode) { | |
case View.MeasureSpec.EXACTLY: | |
height = heightSize; | |
case View.MeasureSpec.AT_MOST: | |
case View.MeasureSpec.UNSPECIFIED: | |
} | |
setMeasuredDimension(widthSize, height); | |
} else { | |
super.onMeasure(recycler, state, widthSpec, heightSpec); | |
} | |
} | |
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, | |
int heightSpec, int[] measuredDimension) { | |
View view = recycler.getViewForPosition(position); | |
// For adding Item Decor Insets to view | |
super.measureChildWithMargins(view, 0, 0); | |
if (view != null) { | |
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams(); | |
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, | |
getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view), p.width); | |
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, | |
getPaddingTop() + getPaddingBottom() + getPaddingBottom() + getDecoratedBottom(view), p.height); | |
view.measure(childWidthSpec, childHeightSpec); | |
// Get decorated measurements | |
measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin; | |
measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin; | |
recycler.recycleView(view); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment