https://my.oschina.net/wanghz/blog/908902
问题一:ScrollView嵌套RecyclerView显示不完全
这个问题应该都遇到过,我之前就被这个问题搞的崩溃了,网上搜的解决方法都是重写LinearLayoutManager,有人说有效,但在我这没用,我也不知道那些方法是不是真的有效,但还是在最下边把这个方法贴出来吧,万一对你有用呢。
真正解决我问题的是另一个方法,不用重写什么布局管理器,只要将嵌套在外层的ScrollView的宽、高均设置为match_parent,然后在ScrollView的布局中加入一句话(必须):
android:fillViewport="true"
然后在RecyclerView的外层再嵌套一个LinearLayout或RelativeLayout即可,这样RecyclerView就可以显示完整。别问我为什么,我也不懂。╮(╯3╰)╭
补充:其实现在直接用android.support.v4.widget.NestedScrollView替代ScrollView,并给RecyclerView设置.setNestedScrollingEnabled(false)属性即可解决
问题二:滑动冲突、滑动不流畅
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
问题三:嵌套布局不显示在顶部,直接显示RecyclerView 第一个item
在Xml页面顶部位置的布局控件中加入两句代码:
Android:focusable="true"
android:focusableInTouchMode="true"
之前还写过一篇解决这类问题的文章:
https://my.oschina.net/wanghz/blog/898256
应该也可以,我没试。
附:重写的LinearLayoutManager布局管理器:
packagecom.wanghz.farver.widget;importandroid.content.Context;importandroid.support.v7.widget.LinearLayoutManager;importandroid.support.v7.widget.RecyclerView;importandroid.util.Log;importandroid.view.View;importandroid.view.ViewGroup;/**
* Created by Wanghz on 2017/5/25.
*/publicclassScrollLinearLayoutManagerextendsLinearLayoutManager{privatestaticfinalString TAG = ScrollLinearLayoutManager.class.getSimpleName();publicScrollLinearLayoutManager(Context context){super(context);}publicScrollLinearLayoutManager(Context context,intorientation,booleanreverseLayout){super(context, orientation, reverseLayout);}privateint[] mMeasuredDimension =newint[2];@OverridepublicvoidonMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,intwidthSpec,intheightSpec){finalintwidthMode = View.MeasureSpec.getMode(widthSpec);finalintheightMode = View.MeasureSpec.getMode(heightSpec);finalintwidthSize = View.MeasureSpec.getSize(widthSpec);finalintheightSize = View.MeasureSpec.getSize(heightSpec);Log.i(TAG,"onMeasure called. \nwidthMode "+ widthMode+" \nheightMode "+ heightSpec+" \nwidthSize "+ widthSize+" \nheightSize "+ heightSize+" \ngetItemCount() "+ getItemCount());intwidth =0;intheight =0;for(inti =0; i < getItemCount(); i++) {measureScrapChild(recycler, i,View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),mMeasuredDimension);if(getOrientation() == HORIZONTAL) {width = width + mMeasuredDimension[0];if(i ==0) {height = mMeasuredDimension[1];}}else{height = height + mMeasuredDimension[1];if(i ==0) {width = mMeasuredDimension[0];}}}switch(widthMode) {caseView.MeasureSpec.EXACTLY:width = widthSize;caseView.MeasureSpec.AT_MOST:caseView.MeasureSpec.UNSPECIFIED:}switch(heightMode) {caseView.MeasureSpec.EXACTLY:height = heightSize;caseView.MeasureSpec.AT_MOST:caseView.MeasureSpec.UNSPECIFIED:}setMeasuredDimension(width, height);}privatevoidmeasureScrapChild(RecyclerView.Recycler recycler,intposition,intwidthSpec,intheightSpec,int[] measuredDimension){try{View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsExceptionif(view !=null) {RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();intchildWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,getPaddingLeft() + getPaddingRight(), p.width);intchildHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,getPaddingTop() + getPaddingBottom(), p.height);view.measure(childWidthSpec, childHeightSpec);measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;recycler.recycleView(view);}}catch(Exception e) {e.printStackTrace();}finally{}}}
然后再在代码中添加:
ScrollLinearLayoutManager linearLayoutManager =newScrollLinearLayoutManager(this); recyclerView.setNestedScrollingEnabled(false);//设置布局管理器 recyclerView.setLayoutManager(linearLayoutManager);
你们可以试试,反正对我没用。