{"id":363,"date":"2023-02-25T08:18:18","date_gmt":"2023-02-25T00:18:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=363"},"modified":"2023-04-30T14:42:51","modified_gmt":"2023-04-30T06:42:51","slug":"recyclerview-implement-maximum-height-maxheight-setting","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/recyclerview-implement-maximum-height-maxheight-setting\/","title":{"rendered":"RecyclerView\u5b9e\u73b0\u8bbe\u7f6e\u6700\u5927\u9ad8\u5ea6maxHeight"},"content":{"rendered":"<p>RecyclerView\u662f\u6ca1\u6709maxHeight\u5c5e\u6027\u914d\u7f6e\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627fRecyclerView\u81ea\u5b9a\u4e49\u5b9e\u73b0\u6b64\u5c5e\u6027\u529f\u80fd\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">public class MaxHeightRecyclerView extends RecyclerView {\n    private int mMaxHeight;\n\n    public MaxHeightRecyclerView(Context context) {\n        super(context);\n    }\n\n    public MaxHeightRecyclerView(Context context, AttributeSet attrs) {\n        super(context, attrs);\n        initialize(context, attrs);\n    }\n\n    public MaxHeightRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n        initialize(context, attrs);\n    }\n\n    private void initialize(Context context, AttributeSet attrs) {\n        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightRecyclerView);\n        mMaxHeight = arr.getLayoutDimension(R.styleable.MaxHeightRecyclerView_maxHeight, mMaxHeight);\n        arr.recycle();\n    }\n\n    @Override\n    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {\n        if (mMaxHeight &gt; 0) {\n            heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST);\n        }\n        super.onMeasure(widthMeasureSpec, heightMeasureSpec);\n    }\n}<\/code><\/pre>\n<p>\u5728style.xml\u6587\u4ef6\u4e2d\u52a0\u5165\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;declare-styleable name=&quot;MaxHeightRecyclerView&quot;&gt;\n    &lt;attr name=&quot;maxHeight&quot; format=&quot;dimension&quot; \/&gt;\n&lt;\/declare-styleable&gt;<\/code><\/pre>\n<p>\u7136\u540e\u5c31\u53ef\u4ee5\u5728xml\u4e2d\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-xml\">app:maxHeight=&quot;400dp&quot;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>RecyclerView\u662f\u6ca1\u6709maxHeight\u5c5e\u6027\u914d\u7f6e\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627fRecyclerView\u81ea\u5b9a\u4e49\u5b9e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[65],"class_list":["post-363","post","type-post","status-publish","format-standard","hentry","category-android-ui","tag-recyclerview"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=363"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/363\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}