{"id":169,"date":"2016-08-17T10:46:50","date_gmt":"2016-08-17T10:46:50","guid":{"rendered":"https:\/\/boochlin.com\/?p=169"},"modified":"2016-08-17T10:46:50","modified_gmt":"2016-08-17T10:46:50","slug":"how-to-use-recyclerview-inside-nestedscrollview","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=169","title":{"rendered":"How to use RecyclerView inside NestedScrollView?"},"content":{"rendered":"<p><a href=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/08\/correct_device.gif\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-177\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/08\/correct_device.gif\" alt=\"correct_device\" width=\"388\" height=\"690\" \/><\/a><\/p>\n<h3>\u5982\u4f55\u5728 NestedScrollView \u5167\u9077\u5165\u00a0RecyclerView \uff1f<\/h3>\n<p>\u4e0a\u9762\u7684\u5716\uff0c\u662f\u6700\u7d42\u5b8c\u6210\u7684\u756b\u9762\u3002<\/p>\n<p>\u81f3\u65bc\u9019\u500b\u554f\u984c\u5c31\u662f\u6c92\u8fa6\u6cd5\u76f4\u63a5\u96a8\u4fbf\u5728 xml \u6a94\u6848\u88e1\u96a8\u4fbf\u5beb\u5beb\u5c31\u53ef\u4ee5\u641e\u5b9a\uff0c\u5982\u4e0b\u3002\u4e3b\u8981\u5c31\u662f RecycleView \u548c NestedScrollView \u7684\u6ed1\u52d5\u6703\u7522\u751f\u885d\u7a81\u3002\u4e26\u975e spec \u6240\u8981\u6c42\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4ee5\u4e0b\u9762\u7684 code \u4f86\u8dd1\u3002<br \/>\n<a href=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/08\/\u87a2\u5e55\u5feb\u7167-2016-08-16-\u4e0b\u53486.54.54.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-174\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/08\/\u87a2\u5e55\u5feb\u7167-2016-08-16-\u4e0b\u53486.54.54.png\" alt=\"\u87a2\u5e55\u5feb\u7167 2016-08-16 \u4e0b\u53486.54.54\" width=\"531\" height=\"837\" \/><\/a><\/p>\n<p>\u5c31\u6703\u8b8a\u6210\u5e95\u4e0b\u9019\u6a23\uff0c<br \/>\n<a href=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/08\/error_recycle.gif\"><img decoding=\"async\" class=\"alignnone size-full wp-image-176\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/08\/error_recycle.gif\" alt=\"error_recycle\" width=\"388\" height=\"690\" \/><\/a><\/p>\n<p>\u4e3b\u8981\u5c31\u662f\u9ad8\u5ea6\u7684\u5224\u5b9a\u6703\u91cd\u758a\u5230\uff0c\u5e95\u4e0b\u7684 RecycleView \u88ab AppBarLayout \u84cb\u4f4f\uff0c\u5728 AppBarLayout \u5c55\u958b\u60c5\u6cc1\u4e0b \uff0c RecycleView \u4e0d\u7ba1\u600e\u9ebc\u6ed1\u52d5\uff0cAppBarLayout \u90fd\u4e0d\u70ba\u6240\u52d5\uff0c\u53ea\u6709\u7576 AppBarLayout \u64c7\u758a\u8d77\u4f86\uff0cRecycleView \u5f80\u4e0b\u62c9\u5230\u5e95\uff0c\u624d\u6703\u5224\u5b9a AppBarLayout \u9023\u52d5\u5230\u3002<\/p>\n<p>\u56e0\u6b64\u9019\u88e1\u53ef\u4ee5\u5408\u7406\u63a8\u65b7 RecycleView \u7684\u9ad8\u5ea6\u5224\u5b9a\u662f\u6709\u554f\u984c\u7684\u3002\u5f9e\u756b\u9762\u4e0a\u4f86\u770b\u767c\u73fe RecycleView \u7684\u986f\u793a\u7bc4\u570d\u662f\u5728\u5f9e AppBarLayout \u5e95\u90e8\u958b\u59cb\u7b97\uff0c\u7136\u5f8c\u52a0\u4e0a RecycleView \u7684\u9ad8\u5ea6\uff0c\u800c RecycleView \u7684\u9ad8\u5ea6\u5247\u662f match_parent \uff0c\u9019\u88e1\u770b\u8d77\u4f86\u662f\u6574\u500b\u87a2\u5e55\u9ad8\u5ea6\uff0c\u90a3\u5c31\u6703\u8b8a\u6210 RecycleView \u7684\u5e95\u90e8\u6703\u8d85\u51fa\u624b\u6a5f\u87a2\u5e55\uff0c\u9019\u4e5f\u53ef\u4ee5\u5f9e\u756b\u9762\u9a57\u8b49\uff0cAppBarLayout \u5c55\u958b\u6642\uff0c RecycleView \u7121\u8ad6\u5982\u4f55\u6ed1\u52d5\u90fd\u7121\u6cd5\u770b\u5230 item 9 , 10 \uff0c\u4f46\u662f\u7576 AppBarLayout \u64c7\u758a\u6642\uff0cRecycleView \u7684\u7bc4\u570d\u5224\u5b9a\u5c31\u662f\u6b63\u5e38\u7684\u3002<\/p>\n<p>\u56e0\u6b64\u8981\u4fee\u6b63\u7684\u9ede\u5c31\u662f\u91cd\u65b0 measure RecycleView \u7684\u7bc4\u570d\uff0c\u9032\u800c\u4fee\u6b63 NestedScrollView \u7684\u884c\u70ba\u3002(\u6709\u95dc <a href=\"https:\/\/segmentfault.com\/a\/1190000002873657\">NestedScrollView<\/a>\u00a0\u9019\u7bc7\u5beb\u5f97\u4e0d\u932f\uff0cNestedScrollView \u51fa\u73fe\u4e3b\u8981\u662f\u60f3\u8981\u89e3\u6c7a\u4f55\u7a2e\u554f\u984c\uff0c\u4e3b\u8981\u662f\u7368\u7acb\u51fa class to handle parent and child touch event \uff0c\u5e38\u642d\u914d CoordinatorLayout \u7684 Behavior \u5b8c\u6210\u9177\u70ab\u529f\u80fd)<\/p>\n<p>\u6709\u95dc RecycleView \u7684 wrap_content \uff0c\u5728 measure child \u6642\uff0c\u6703\u51fa\u73fe size = 0 \u7684\u60c5\u6cc1\u3002\u9019\u5c31\u7167\u6210\u5f8c\u7e8c\u7684\u554f\u984c\uff08<a href=\"http:\/\/RecyclerView cannot size itself based on the measured dimensions of its children\">\u5927\u795e\u539f\u59cb\u554f\u984c<\/a>\uff0c\u5df2\u7d93\u56de\u5831\u7d66 google\uff09\uff0c\u56e0\u6b64\u9019\u88e1\u63d0\u51fa\u91cd\u5beb onMeasure \u7684\u65b9\u6cd5\uff0c\u4e3b\u8981\u6539\u6210\u7528\u00a0<span class=\"pl-smi\">View<\/span><span class=\"pl-k\">.<\/span><span class=\"pl-smi\">MeasureSpec<\/span><span class=\"pl-c1\"><span class=\"pl-k\">.<\/span>UNSPECIFIED \u53bb\u8b93 child \u81ea\u5df1\u6c7a\u5b9a\u9ad8\u5ea6\uff0c\u6700\u5f8c\u52a0\u7e3d\u5f97\u5230 RecycleView \u7684\u9ad8\u5ea6\u3002<\/span><\/p>\n<p><span style=\"color: #ff0000;\">ps. \u4e0a\u8ff0\u554f\u984c\u6703\u767c\u751f\u5728 android support library 23.2 \u4e4b\u524d\u3002\u5982\u679c\u662f\u4f7f\u7528 23.2 (\u5305\u542b\uff09\u4e4b\u5f8c\u7684 RecycleView \u4e4b\u5f8c\uff0c\u5c31\u53ef\u4ee5\u7528\u53e6\u5916\u4e00\u5957\u4fee\u6b63\u65b9\u6cd5(\u8df3\u904e\u4e0b\u9762\u9019\u6bb5\u7a0b\u5f0f\u78bc)<\/span><\/p>\n<p>ref\uff1a<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/31000081\/how-to-use-recyclerview-inside-nestedscrollview\">http:\/\/stackoverflow.com\/questions\/31000081\/how-to-use-recyclerview-inside-nestedscrollview<\/a><br \/>\n<a href=\"https:\/\/github.com\/emanuelet\/LayoutManagers\/blob\/master\/ExpansiveLayoutManager.java\">https:\/\/github.com\/emanuelet\/LayoutManagers\/blob\/master\/ExpansiveLayoutManager.java<\/a><br \/>\n<a href=\"http:\/\/blog.csdn.net\/yaochangliang159\/article\/details\/50540276\">http:\/\/blog.csdn.net\/yaochangliang159\/article\/details\/50540276<\/a><\/p>\n<pre>public class ExpandBarLinearLayoutManager extends LinearLayoutManager {\r\n\r\n    public ExpandBarLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {\r\n        super(context, orientation, reverseLayout);\r\n    }\r\n\r\n    private int[] mMeasuredDimension = new int[2];\r\n\r\n    @Override\r\n    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,\r\n                          int widthSpec, int heightSpec) {\r\n        final int widthMode = View.MeasureSpec.getMode(widthSpec);\r\n        final int heightMode = View.MeasureSpec.getMode(heightSpec);\r\n        final int widthSize = View.MeasureSpec.getSize(widthSpec);\r\n        final int heightSize = View.MeasureSpec.getSize(heightSpec);\r\n        int width = 0;\r\n        int height = 0;\r\n        for (int i = 0; i &lt; getItemCount(); i++) {\r\n\r\n            if (getOrientation() == HORIZONTAL) {\r\n\r\n                measureScrapChild(recycler, i,\r\n                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),\r\n                        heightSpec,\r\n                        mMeasuredDimension);\r\n\r\n                width = width + mMeasuredDimension[0];\r\n                if (i == 0) {\r\n                    height = mMeasuredDimension[1];\r\n                }\r\n            } else {\r\n                measureScrapChild(recycler, i,\r\n                        widthSpec,\r\n                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),\r\n                        mMeasuredDimension);\r\n                height = height + mMeasuredDimension[1];\r\n                if (i == 0) {\r\n                    width = mMeasuredDimension[0];\r\n                }\r\n            }\r\n        }\r\n        switch (widthMode) {\r\n            case View.MeasureSpec.EXACTLY:\r\n                width = widthSize;\r\n            case View.MeasureSpec.AT_MOST:\r\n            case View.MeasureSpec.UNSPECIFIED:\r\n        }\r\n\r\n        switch (heightMode) {\r\n            case View.MeasureSpec.EXACTLY:\r\n                height = heightSize;\r\n            case View.MeasureSpec.AT_MOST:\r\n            case View.MeasureSpec.UNSPECIFIED:\r\n        }\r\n\r\n        setMeasuredDimension(width, height);\r\n    }\r\n\r\n    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,\r\n                                   int heightSpec, int[] measuredDimension) {\r\n        View view = recycler.getViewForPosition(position);\r\n        recycler.bindViewToPosition(view, position);\r\n        if (view != null) {\r\n            RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();\r\n            int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,\r\n                    getPaddingLeft() + getPaddingRight(), p.width);\r\n            int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,\r\n                    getPaddingTop() + getPaddingBottom(), p.height);\r\n            view.measure(childWidthSpec, childHeightSpec);\r\n            measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;\r\n            measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;\r\n            recycler.recycleView(view);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u7136\u5f8c\u4e00\u5b9a\u8981\u5e6b RecycleView \u8a2d\u5b9a setNestedScrollingEnabled(false)<\/p>\n<pre>recycleView.setLayoutManager(new ExpandBarLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));\r\nrecycleView.setNestedScrollingEnabled(false);\r\n<\/pre>\n<p>\u7136\u5f8c\u5982\u679c\u76f4\u63a5\u4f7f\u7528 23.2 \u4e4b\u5f8c\u7684 RecycleView \u7684\u4eba\uff0c google \u5df2\u7d93\u4fee\u6b63\u4e86\uff0c\u53ea\u9700\u8981<\/p>\n<pre>LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());\r\n\/\/ \u5176\u5be6\u9ed8\u8a8d auto measure is true.\r\nlinearLayoutManager.setAutoMeasureEnabled(true);\r\nrecycleView.setLayoutManager(linearLayoutManager);\r\n\/\/ \u9019\u884c\u4e0d\u80fd\u5c11\r\nrecycleView.setNestedScrollingEnabled(false);\r\n<\/pre>\n<p>\u9019\u88e1\u6211\u770b\u539f\u59cb\u78bc\uff0c\u9019\u4e00\u6bb5\u5c31\u662f\u4fee\u6b63 code \uff0c\u9084\u883b\u76f4\u89ba\uff0c\u4e26\u975e\u76f4\u63a5\u5f37\u5236\u4f7f\u7528 View.MeasureSpec.UNSPECIFIED\uff08\u9019\u554f\u984c\u9084\u883b\u660e\u986f\u7684\uff0c\u9019\u6703\u4f7f\u5f97 child view measure \u98a8\u96aa\u8b8a\u5f88\u5927\uff0c\u56e0\u70ba\u7121\u6cd5\u63a7\u5236 child view \u662f\u9069\u5408\u54ea\u4e00\u7a2e\uff09\uff0c\u800c\u662f\u63a1\u7528\u5169\u968e\u6bb5 layout \uff0c\u85c9\u6b64\u5f97\u5230\u6b63\u78ba\u7684 child size.<\/p>\n<pre>      if (mLayout.mAutoMeasure) {\r\n            final int widthMode = MeasureSpec.getMode(widthSpec);\r\n            final int heightMode = MeasureSpec.getMode(heightSpec);\r\n            final boolean skipMeasure = widthMode == MeasureSpec.EXACTLY\r\n                    &amp;&amp; heightMode == MeasureSpec.EXACTLY;\r\n            mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);\r\n            if (skipMeasure || mAdapter == null) {\r\n                return;\r\n            }\r\n            if (mState.mLayoutStep == State.STEP_START) {\r\n                dispatchLayoutStep1();\r\n            }\r\n            \/\/ set dimensions in 2nd step. Pre-layout should happen with old dimensions for\r\n            \/\/ consistency\r\n            mLayout.setMeasureSpecs(widthSpec, heightSpec);\r\n            mState.mIsMeasuring = true;\r\n            dispatchLayoutStep2();\r\n\r\n            \/\/ now we can get the width and height from the children.\r\n            mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec);\r\n\r\n            \/\/ if RecyclerView has non-exact width and height and if there is at least one child\r\n            \/\/ which also has non-exact width &amp; height, we have to re-measure.\r\n            if (mLayout.shouldMeasureTwice()) {\r\n                mLayout.setMeasureSpecs(\r\n                        MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),\r\n                        MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));\r\n                mState.mIsMeasuring = true;\r\n                dispatchLayoutStep2();\r\n                \/\/ now we can get the width and height from the children.\r\n                mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec);\r\n            }\r\n        }\r\n<\/pre>\n<p>\u6709\u8208\u8da3\u7684\u4eba\uff0c\u53ef\u4ee5\u7e7c\u7e8c trace code \uff0c\u4f46\u6211\u5c31\u5230\u6b64\u4e00\u97a0\u8eac\u5566\u3002<\/p>\n<p>other reference:<\/p>\n<p><a href=\"http:\/\/www.jianshu.com\/p\/4535442d568f\">http:\/\/www.jianshu.com\/p\/4535442d568f<\/a><\/p>\n<p><a href=\"http:\/\/www.jianshu.com\/p\/06c0ae8d9a96\">http:\/\/www.jianshu.com\/p\/06c0ae8d9a96<\/a><\/p>\n<p><a href=\"http:\/\/blog.csdn.net\/feiduclear_up\/article\/details\/46514791\">http:\/\/blog.csdn.net\/feiduclear_up\/article\/details\/46514791<\/a><\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/25702884\/add-viewpagerindicator-to-android-studio\">http:\/\/stackoverflow.com\/questions\/25702884\/add-viewpagerindicator-to-android-studio<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 NestedScrollView \u5167\u9077\u5165\u00a0RecyclerView \uff1f \u4e0a\u9762\u7684\u5716\uff0c\u662f\u6700\u7d42\u5b8c\u6210\u7684\u756b\u9762\u3002 \u81f3\u65bc\u9019\u500b\u554f\u984c\u5c31\u662f\u6c92\u8fa6\u6cd5\u76f4\u63a5\u96a8\u4fbf\u5728 xml \u6a94\u6848\u88e1\u96a8\u4fbf\u5beb\u5beb\u5c31\u53ef\u4ee5\u641e\u5b9a\uff0c\u5982\u4e0b\u3002\u4e3b\u8981\u5c31\u662f Rec&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[6,59,58],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/169"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=169"}],"version-history":[{"count":0,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/169\/revisions"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}