Last active
September 9, 2016 06:07
-
-
Save samuel22gj/a5ffdef1ea534bedfd38f79732364688 to your computer and use it in GitHub Desktop.
View
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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<attr name="maxHeight" format="dimension" /> | |
<attr name="maxWidth" format="dimension" /> | |
<declare-styleable name="MyRecyclerView"> | |
<attr name="maxHeight" /> | |
<attr name="maxWidth" /> | |
</declare-styleable> | |
</resources> |
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
public class MyRecyclerView extends RecyclerView { | |
private static final String TAG = MyRecyclerView.class.getSimpleName(); | |
private int mMaxWidth = Integer.MAX_VALUE; | |
private int mMaxHeight = Integer.MAX_VALUE; | |
public MyRecyclerView(Context context) { | |
this(context, null); | |
} | |
public MyRecyclerView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public MyRecyclerView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(context, attrs, defStyle); | |
} | |
private void init(Context context, AttributeSet attrs, int defStyle) { | |
final TypedArray typedArray = context.obtainStyledAttributes( | |
attrs, R.styleable.MyRecyclerView, defStyle, 0); | |
setMaxWidth(typedArray.getDimensionPixelSize(R.styleable.MyRecyclerView_maxWidth, Integer.MAX_VALUE)); | |
setMaxHeight(typedArray.getDimensionPixelSize(R.styleable.MyRecyclerView_maxHeight, Integer.MAX_VALUE)); | |
typedArray.recycle(); | |
} | |
/** | |
* Supply a maximum width for this view. | |
* | |
* @param maxWidth The maximum width in pixels. | |
* | |
* @see #getMaxWidth() | |
*/ | |
public void setMaxWidth(int maxWidth) { | |
mMaxWidth = maxWidth; | |
} | |
/** | |
* @return The maximum width of this view in pixels. | |
* | |
* @see #setMaxWidth(int) | |
*/ | |
public int getMaxWidth() { | |
return mMaxWidth; | |
} | |
/** | |
* Supply a maximum height for this view. | |
* | |
* @param maxHeight The maximum height in pixels. | |
* | |
* @see #getMaxHeight() | |
*/ | |
public void setMaxHeight(int maxHeight) { | |
mMaxHeight = maxHeight; | |
} | |
/** | |
* @return The maximum height of this view in pixels. | |
* | |
* @see #setMaxHeight(int) | |
*/ | |
public int getMaxHeight() { | |
return mMaxHeight; | |
} | |
@Override | |
protected void onMeasure(int widthSpec, int heightSpec) { | |
int width = MeasureSpec.getSize(widthSpec); | |
int height = MeasureSpec.getSize(heightSpec); | |
if (mMaxWidth >= 0 && width > mMaxWidth) { | |
widthSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, MeasureSpec.AT_MOST); | |
} | |
if (mMaxHeight >= 0 && height > mMaxHeight) { | |
heightSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST); | |
} | |
super.onMeasure(widthSpec, heightSpec); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment