https://my.oschina.net/wanghz/blog/908902
問題一:ScrollView嵌套R(shí)ecyclerView顯示不完全
這個(gè)問題應(yīng)該都遇到過放钦,我之前就被這個(gè)問題搞的崩潰了墨微,網(wǎng)上搜的解決方法都是重寫LinearLayoutManager,有人說有效,但在我這沒用买喧,我也不知道那些方法是不是真的有效,但還是在最下邊把這個(gè)方法貼出來吧螃成,萬一對(duì)你有用呢犹菱。
真正解決我問題的是另一個(gè)方法,不用重寫什么布局管理器颊乘,只要將嵌套在外層的ScrollView的寬参淹、高均設(shè)置為match_parent,然后在ScrollView的布局中加入一句話(必須):
android:fillViewport="true"
然后在RecyclerView的外層再嵌套一個(gè)LinearLayout或RelativeLayout即可乏悄,這樣RecyclerView就可以顯示完整浙值。別問我為什么,我也不懂檩小。╮(╯3╰)╭
補(bǔ)充:其實(shí)現(xiàn)在直接用android.support.v4.widget.NestedScrollView替代ScrollView开呐,并給RecyclerView設(shè)置.setNestedScrollingEnabled(false)屬性即可解決
問題二:滑動(dòng)沖突、滑動(dòng)不流暢
? ? ? ? recyclerView.setHasFixedSize(true);
? ? ? ??recyclerView.setNestedScrollingEnabled(false)规求;
問題三:嵌套布局不顯示在頂部筐付,直接顯示RecyclerView 第一個(gè)item
????????在Xml頁面頂部位置的布局控件中加入兩句代碼:
? ? ? ??Android:focusable="true"
? ? ? ? android:focusableInTouchMode="true"?
? ? ? ? 之前還寫過一篇解決這類問題的文章:
????????https://my.oschina.net/wanghz/blog/898256
? ? ? ? 應(yīng)該也可以,我沒試阻肿。
附:重寫的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 動(dòng)態(tài)添加時(shí)報(bào)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);//設(shè)置布局管理器? recyclerView.setLayoutManager(linearLayoutManager);
你們可以試試瓦戚,反正對(duì)我沒用。