android recycle高度,Android RecycleView增加最大高度和宽度属性

本文介绍了如何在Android开发中通过自定义RecyclerView并覆盖onMeasure方法,实现实时限制RecyclerView的最大高度和宽度。通过设置maxHeight和maxWidth属性,可以在布局文件中方便地控制RecyclerView的显示范围。
摘要由CSDN通过智能技术生成

一、前言:

在日常开发中,想直接通过android:maxHeight或android:maxWidth在布局文件中限制RecyclerView的最大高度宽度,是无法实现的。通过自定义RecyclerView,覆盖onMeasure方法。在onMeasure方法内部,当发现自身高度或宽度超过限制的最大高度或宽度,则手动将宽或高设置为期望的最大宽或搞。具体代码实现如下:

1、自定义MaxLimitRecyclerView

/**

* max limit-able RecyclerView

*/

public class MaxLimitRecyclerView extends RecyclerView {

private int mMaxHeight;

private int mMaxWidth;

public MaxLimitRecyclerView(Context context) {

this(context, null);

}

public MaxLimitRecyclerView(Context context, @Nullable AttributeSet attrs) {

this(context, attrs, 0);

}

public MaxLimitRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

inti(attrs);

}

private void inti(AttributeSet attrs) {

if (getContext() != null && attrs != null) {

TypedArray typedArray = null;

try {

typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MaxLimitRecyclerView);

if (typedArray.hasValue(R.styleable.MaxLimitRecyclerView_limit_maxHeight)) {

mMaxHeight = typedArray.getDimensionPixelOffset(R.styleable.MaxLimitRecyclerView_limit_maxHeight, -1);

}

if (typedArray.hasValue(R.styleable.MaxLimitRecyclerView_limit_maxWidth)) {

mMaxWidth = typedArray.getDimensionPixelOffset(R.styleable.MaxLimitRecyclerView_limit_maxWidth, -1);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (typedArray != null) {

typedArray.recycle();

}

}

}

}

@Override

protected void onMeasure(int widthSpec, int heightSpec) {

super.onMeasure(widthSpec, heightSpec);

boolean needLimit = false;

if (mMaxHeight >= 0 || mMaxWidth >= 0) {

needLimit = true;

}

if (needLimit) {

int limitHeight = getMeasuredHeight();

int limitWith = getMeasuredWidth();

if (getMeasuredHeight() > mMaxHeight) {

limitHeight = mMaxHeight;

}

if (getMeasuredWidth() > mMaxWidth) {

limitWith = mMaxWidth;

}

setMeasuredDimension(limitWith, limitHeight);

}

}

}

2、对应属性xml

注意:在res下的values中创建attrs.xml,导入下面的代码

733a5e9b83c4

图片.png

3、使用

android:id="@+id/act_choose_list"

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:limit_maxHeight="@dimen/dp_255"

app:limit_maxWidth="@dimen/dp_275"

tools:itemCount="8" />

必须指定RecyclerView的最大值和最小值

app:limit_maxHeight="@dimen/dp_255"

app:limit_maxWidth="@dimen/dp_275"

以上,便可实现对RecyclerView最大高度或宽度的限制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以自定义一个继承自FrameLayout的类,在类中添加设置最大宽度最大高度的方法,同时在类中添加相关的XML属性。代码示例如下: ```java public class CustomFrameLayout extends FrameLayout { private int mMaxWidth; private int mMaxHeight; public CustomFrameLayout(Context context) { super(context); } public CustomFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); initAttrs(context, attrs); } public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initAttrs(context, attrs); } private void initAttrs(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFrameLayout); mMaxWidth = a.getDimensionPixelSize(R.styleable.CustomFrameLayout_maxWidth, 0); mMaxHeight = a.getDimensionPixelSize(R.styleable.CustomFrameLayout_maxHeight, 0); a.recycle(); } /** * 设置最大宽度 * * @param maxWidth 最大宽度 */ public void setMaxWidth(int maxWidth) { mMaxWidth = maxWidth; requestLayout(); } /** * 设置最大高度 * * @param maxHeight 最大高度 */ public void setMaxHeight(int maxHeight) { mMaxHeight = maxHeight; requestLayout(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); // 如果设置了最大宽度,则取最大宽度与父容器宽度的最小值 if (mMaxWidth > 0 && widthMode != MeasureSpec.UNSPECIFIED) { widthSize = Math.min(widthSize, mMaxWidth); } // 如果设置了最大高度,则取最大高度与父容器高度的最小值 if (mMaxHeight > 0 && heightMode != MeasureSpec.UNSPECIFIED) { heightSize = Math.min(heightSize, mMaxHeight); } setMeasuredDimension(widthSize, heightSize); } } ``` 在res/values/attrs.xml文件中添加自定义属性: ```xml <declare-styleable name="CustomFrameLayout"> <attr name="maxWidth" format="dimension" /> <attr name="maxHeight" format="dimension" /> </declare-styleable> ``` 然后在布局文件中使用自定义FrameLayout并设置相关属性即可: ```xml <com.example.CustomFrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:maxWidth="500dp" app:maxHeight="500dp"> <!-- 添加子View --> </com.example.CustomFrameLayout> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值