关于ScrollView嵌套RecyclerView时RecyclerView不显示的问题

 
 

在新版本中需求变更导致布局需要变化,RecyclerView外需要一层ScrollView来处理滑动。发布前夕发现在API 23 & 24上RecyclerView显示不完整。

  光速冷静下来,马上去stackoverflow翻了一下,有人说ScrollView加上 android:fillViewport="true" ,但是我加上并没有解决问题。后来在RecyclerView外面加了一层RelativeLayout,问题解决。如果你在API 23 & 24上也遇到这个问题,可以参考一下。

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <!-- DEV NOTE: Outer wrapper relative layout is added intentionally to address issue
                 that only happens on Marshmallow & Nougat devices (API 23 & 24).
                 On marshmallow API 23, the "RecyclerView" `layout_height="wrap_content"` does NOT
                 occupy the height of all the elements added to it via adapter. The result is cut out
                 items that is outside of device viewport when it loads initially.
                 Wrapping "RecyclerView" with "RelativeLayout" fixes the issue on Marshmallow devices.
            -->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.jcodecraeer.xrecyclerview.XRecyclerView
                android:id="@+id/rv_fragment_find_tips_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                />
        </RelativeLayout>


    </android.support.v4.widget.NestedScrollView>










  1. import android.content.Context;  
  2. import android.support.v7.widget.LinearLayoutManager;  
  3. import android.support.v7.widget.RecyclerView;  
  4. import android.util.Log;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7.   
  8. /**  
  9.  * Created by k on 2016/8/13.  
  10.  */  
  11. public class FullyLinearLayoutManager extends LinearLayoutManager {  
  12.   
  13.     private static final String TAG = FullyLinearLayoutManager.class.getSimpleName();  
  14.   
  15.     public FullyLinearLayoutManager(Context context) {  
  16.         super(context);  
  17.     }  
  18.   
  19.     public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {  
  20.         super(context, orientation, reverseLayout);  
  21.     }  
  22.   
  23.     private int[] mMeasuredDimension = new int[2];  
  24.   
  25.     @Override  
  26.     public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,  
  27.                           int widthSpec, int heightSpec) {  
  28.   
  29.         final int widthMode = View.MeasureSpec.getMode(widthSpec);  
  30.         final int heightMode = View.MeasureSpec.getMode(heightSpec);  
  31.         final int widthSize = View.MeasureSpec.getSize(widthSpec);  
  32.         final int heightSize = View.MeasureSpec.getSize(heightSpec);  
  33.   
  34.         Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode  
  35.                 + " \nheightMode " + heightSpec  
  36.                 + " \nwidthSize " + widthSize  
  37.                 + " \nheightSize " + heightSize  
  38.                 + " \ngetItemCount() " + getItemCount());  
  39.   
  40.         int width = 0;  
  41.         int height = 0;  
  42.         for (int i = 0; i < getItemCount(); i++) {  
  43.             measureScrapChild(recycler, i,  
  44.                     View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),  
  45.                     View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),  
  46.                     mMeasuredDimension);  
  47.   
  48.             if (getOrientation() == HORIZONTAL) {  
  49.                 width = width + mMeasuredDimension[0];  
  50.                 if (i == 0) {  
  51.                     height = mMeasuredDimension[1];  
  52.                 }  
  53.             } else {  
  54.                 height = height + mMeasuredDimension[1];  
  55.                 if (i == 0) {  
  56.                     width = mMeasuredDimension[0];  
  57.                 }  
  58.             }  
  59.         }  
  60.         switch (widthMode) {  
  61.             case View.MeasureSpec.EXACTLY:  
  62.                 width = widthSize;  
  63.             case View.MeasureSpec.AT_MOST:  
  64.             case View.MeasureSpec.UNSPECIFIED:  
  65.         }  
  66.   
  67.         switch (heightMode) {  
  68.             case View.MeasureSpec.EXACTLY:  
  69.                 height = heightSize;  
  70.             case View.MeasureSpec.AT_MOST:  
  71.             case View.MeasureSpec.UNSPECIFIED:  
  72.         }  
  73.   
  74.         setMeasuredDimension(width, height);  
  75.     }  
  76.   
  77.     private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,  
  78.                                    int heightSpec, int[] measuredDimension) {  
  79.         try {  
  80.             View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException  
  81.   
  82.             if (view != null) {  
  83.                 RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();  
  84.   
  85.                 int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,  
  86.                         getPaddingLeft() + getPaddingRight(), p.width);  
  87.   
  88.                 int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,  
  89.                         getPaddingTop() + getPaddingBottom(), p.height);  
  90.   
  91.                 view.measure(childWidthSpec, childHeightSpec);  
  92.                 measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;  
  93.                 measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;  
  94.                 recycler.recycleView(view);  
  95.             }  
  96.         } catch (Exception e) {  
  97.             e.printStackTrace();  
  98.         } finally {  
  99.         }  
  100.     }  
  101. }  

然后在代码里添加如下:

[html]  view plain  copy
  1. FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(this);  
  2.         recyclerView.setNestedScrollingEnabled(false);  
  3.         //设置布局管理器  
  4.         recyclerView.setLayoutManager(linearLayoutManager);  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值