解决Scrollview 嵌套recyclerview不能显示,高度不正常的问题

我们先看一个效果,问题说的就是中间的Grid效果在Scrollview 嵌套recyclerview显示问题,在Android Api 24是好的,不过在5,1,1版本(api 22)缺出现了问题

解决Scrollview 嵌套recyclerview不能显示,高度不正常的问题_第1张图片

最近项目中,有一个商品详情页面,页面有好几个网格页面,大家说,我们大可以用GridView去做,但是需要方的要求是,我们的网格的中间的线怎么做呢,对于GridView,我们知道我们可以这是一个背景,然后用verticalSpacing来做,这也算一个方法吧,但是对于Line线的计算是一个问题,有很多的计算逻辑,这样对代码的美观就造成了破坏,且看一段之前的代码:

[html] view plain copy
print ?
  1. private void computeCompanyGridViewHeight(int size) {  
  2.         S.p("computeCompanyGridViewHeight size:" + size);  
  3.         if (size > 9) {  
  4.             size = 9;  
  5.         }  
  6.         int len = 3;  
  7.         if (size % 3 == 0) {  
  8.             len = size / 3 - 1;  
  9.         } else {  
  10.             len = size / 3;  
  11.         }  
  12.         int height = (len + 1) * Utils.dip2px(getActivity(), 58);  
  13.         ship_mid_companys.getLayoutParams().height = height;  
  14.     }  
private void computeCompanyGridViewHeight(int size) {
		S.p("computeCompanyGridViewHeight size:" + size);
		if (size > 9) {
			size = 9;
		}
		int len = 3;
		if (size % 3 == 0) {
			len = size / 3 - 1;
		} else {
			len = size / 3;
		}
		int height = (len + 1) * Utils.dip2px(getActivity(), 58);
		ship_mid_companys.getLayoutParams().height = height;
	}

我们这里采用RecycleView来实现。以前在ScrollView中嵌套嵌套ListView,无法正确的计算ListView的大小,现在我们在ScrollView中嵌套嵌套RecycleView的时候,也出现了计算不出高度的问题,于是有人想到我们是不是可以自己实现一个重写一个继承自RecycleView的类,重写OmMeasure,呵呵,但是实际上这是不行的,RecycleView是具体的一个控件,不相同与我们的ListView,这里参照之前网上的解决方案,我们可以继承自GridManager,然后对OnMeasure重写,其他的列表效果如此,

[html] view plain copy
print ?
  1. public class WrappingGridLayoutManager extends GridLayoutManager {  
  2.   
  3.         private int mwidth = 0;  
  4.         private int mheight = 0;  
  5.   
  6.         public WrappingGridLayoutManager(Context context, int spanCount) {  
  7.             super(context, spanCount);  
  8.         }  
  9.   
  10.         public WrappingGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {  
  11.             super(context, spanCount, orientation, reverseLayout);  
  12.         }  
  13.   
  14.         private int[] mMeasuredDimension = new int[2];  
  15.   
  16.         public int getMwidth() {  
  17.             return mwidth;  
  18.         }  
  19.   
  20.         public void setMwidth(int mwidth) {  
  21.             this.mwidth = mwidth;  
  22.         }  
  23.   
  24.         public int getMheight() {  
  25.             return mheight;  
  26.         }  
  27.   
  28.         public void setMheight(int mheight) {  
  29.             this.mheight = mheight;  
  30.         }  
  31.   
  32.         @Override  
  33.         public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {  
  34.             final int widthMode = View.MeasureSpec.getMode(widthSpec);  
  35.             final int heightMode = View.MeasureSpec.getMode(heightSpec);  
  36.             final int widthSize = View.MeasureSpec.getSize(widthSpec);  
  37.             final int heightSize = View.MeasureSpec.getSize(heightSpec);  
  38.   
  39.             int width = 0;  
  40.             int height = 0;  
  41.             int count = getItemCount();  
  42.             int span = getSpanCount();  
  43.            <span style="color:#ff0000;"> for (int i = 0; i < count; i++) {  
  44.                 measureScrapChild(recycler, i,  
  45.                         View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),  
  46.                         View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),  
  47.                         mMeasuredDimension);span>  
  48.   
  49.                 if (getOrientation() == HORIZONTAL) {  
  50.                     if (i % span == 0) {  
  51.                         width = width + mMeasuredDimension[0];  
  52.                     }  
  53.                     if (i == 0) {  
  54.                         height = mMeasuredDimension[1];  
  55.                     }  
  56.                 } else {  
  57.                     if (i % span == 0) {  
  58.                         height = height + mMeasuredDimension[1];  
  59.                     }  
  60.                     if (i == 0) {  
  61.                         width = mMeasuredDimension[0];  
  62.                     }  
  63.                 }  
  64.             }  
  65.   
  66.             switch (widthMode) {  
  67.                 case View.MeasureSpec.EXACTLY:  
  68.                     width = widthSize;  
  69.                 case View.MeasureSpec.AT_MOST:  
  70.                 case View.MeasureSpec.UNSPECIFIED:  
  71.             }  
  72.   
  73.             switch (heightMode) {  
  74.                 case View.MeasureSpec.EXACTLY:  
  75.                     height = heightSize;  
  76.                 case View.MeasureSpec.AT_MOST:  
  77.                 case View.MeasureSpec.UNSPECIFIED:  
  78.             }  
  79.             setMheight(height);  
  80.             setMwidth(width);  
  81.             setMeasuredDimension(width, height);  
  82.         }  
  83.   
  84.         private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,  
  85.                                        int heightSpec, int[] measuredDimension) {  
  86.             if (position < getItemCount()) {  
  87.                 try {  
  88.                     View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException  
  89.                     if (view != null) {  
  90.                         RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();  
  91.                         int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,  
  92.                                 getPaddingLeft() + getPaddingRight(), p.width);  
  93.                         int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,  
  94.                                 getPaddingTop() + getPaddingBottom(), p.height);  
  95.                         view.measure(childWidthSpec, childHeightSpec);  
  96.                         measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;  
  97.                         measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;  
  98.                         recycler.recycleView(view);  
  99.                     }  
  100.                 } catch (Exception e) {  
  101.                     e.printStackTrace();  
  102.                 }  
  103.             }  
  104.     }  
  105. }  
public class WrappingGridLayoutManager extends GridLayoutManager {

        private int mwidth = 0;
        private int mheight = 0;

        public WrappingGridLayoutManager(Context context, int spanCount) {
            super(context, spanCount);
        }

        public WrappingGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
            super(context, spanCount, orientation, reverseLayout);
        }

        private int[] mMeasuredDimension = new int[2];

        public int getMwidth() {
            return mwidth;
        }

        public void setMwidth(int mwidth) {
            this.mwidth = mwidth;
        }

        public int getMheight() {
            return mheight;
        }

        public void setMheight(int mheight) {
            this.mheight = mheight;
        }

        @Override
        public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
            final int widthMode = View.MeasureSpec.getMode(widthSpec);
            final int heightMode = View.MeasureSpec.getMode(heightSpec);
            final int widthSize = View.MeasureSpec.getSize(widthSpec);
            final int heightSize = View.MeasureSpec.getSize(heightSpec);

            int width = 0;
            int height = 0;
            int count = getItemCount();
            int span = getSpanCount();
            for (int i = 0; i < count; i++) {
                measureScrapChild(recycler, i,
                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                        mMeasuredDimension);

                if (getOrientation() == HORIZONTAL) {
                    if (i % span == 0) {
                        width = width + mMeasuredDimension[0];
                    }
                    if (i == 0) {
                        height = mMeasuredDimension[1];
                    }
                } else {
                    if (i % span == 0) {
                        height = height + mMeasuredDimension[1];
                    }
                    if (i == 0) {
                        width = mMeasuredDimension[0];
                    }
                }
            }

            switch (widthMode) {
                case View.MeasureSpec.EXACTLY:
                    width = widthSize;
                case View.MeasureSpec.AT_MOST:
                case View.MeasureSpec.UNSPECIFIED:
            }

            switch (heightMode) {
                case View.MeasureSpec.EXACTLY:
                    height = heightSize;
                case View.MeasureSpec.AT_MOST:
                case View.MeasureSpec.UNSPECIFIED:
            }
            setMheight(height);
            setMwidth(width);
            setMeasuredDimension(width, height);
        }

        private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                       int heightSpec, int[] measuredDimension) {
            if (position < getItemCount()) {
                try {
                    View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
                    if (view != null) {
                        RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
                        int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                                getPaddingLeft() + getPaddingRight(), p.width);
                        int childHeightSpec = 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();
                }
            }
    }
}
[html] view plain copy
print ?
  1. private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,  
  2.                                       int heightSpec, int[] measuredDimension) {  
  3.            if (position < getItemCount()) {  
  4.                try {  
  5.                    View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException  
  6.                    if (view != null) {  
  7.                        RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();  
  8.                        int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,  
  9.                                getPaddingLeft() + getPaddingRight(), p.width);  
  10.                        int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,  
  11.                                getPaddingTop() + getPaddingBottom(), p.height);  
  12.                        view.measure(childWidthSpec, childHeightSpec);  
  13.                        measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;  
  14.                        measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;  
  15.                        recycler.recycleView(view);  
  16.                    }  
  17.                } catch (Exception e) {  
  18.                    e.printStackTrace();  
  19.                }  
  20.            }  
  21.    }  
 private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                       int heightSpec, int[] measuredDimension) {
            if (position < getItemCount()) {
                try {
                    View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
                    if (view != null) {
                        RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
                        int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                                getPaddingLeft() + getPaddingRight(), p.width);
                        int childHeightSpec = 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();
                }
            }
    }
至于划线,我们需要另一个类实现,这用到了Recycle的一个方法

[html] view plain copy
print ?
  1. recyclerView.addItemDecoration(new SupportGridItemLine(this));  
recyclerView.addItemDecoration(new SupportGridItemLine(this));

Line线计算类:

[html] view plain copy
print ?
  1. public class SupportGridItemLine extends RecyclerView.ItemDecoration {  
  2.   
  3.     private static final int[] ATTRS = new int[]{android.R.attr.listDivider};  
  4.     private Drawable mDivider;  
  5.   
  6.     public SupportGridItemLine(Context context) {  
  7.         final TypedArray a = context.obtainStyledAttributes(ATTRS);  
  8.         mDivider = a.getDrawable(0);  
  9.         a.recycle();  
  10.     }  
  11.   
  12.     @Override  
  13.     public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {  
  14.   
  15.         drawHorizontal(c, parent);  
  16.         drawVertical(c, parent);  
  17.   
  18.     }  
  19.   
  20.     private int getSpanCount(RecyclerView parent) {  
  21.         // 列数  
  22.         int spanCount = -1;  
  23.         RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();  
  24.         if (layoutManager instanceof GridLayoutManager) {  
  25.   
  26.             spanCount = ((GridLayoutManager) layoutManager).getSpanCount();  
  27.         } else if (layoutManager instanceof StaggeredGridLayoutManager) {  
  28.             spanCount = ((StaggeredGridLayoutManager) layoutManager)  
  29.                     .getSpanCount();  
  30.         }  
  31.         return spanCount;  
  32.     }  
  33.   
  34.     public void drawHorizontal(Canvas c, RecyclerView parent) {  
  35.         int childCount = parent.getChildCount();  
  36.         for (int i = 0; i < childCount; i++) {  
  37.             final View child = parent.getChildAt(i);  
  38.             final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child  
  39.                     .getLayoutParams();  
  40.             final int left = child.getLeft() - params.leftMargin;  
  41.             final int right = child.getRight() + params.rightMargin  
  42.                     + mDivider.getIntrinsicWidth();  
  43.             final int top = child.getBottom() + params.bottomMargin;  
  44.             final int bottom = top + mDivider.getIntrinsicHeight();  
  45.             mDivider.setBounds(left, top, right, bottom);  
  46.             mDivider.draw(c);  
  47.         }  
  48.     }  
  49.   
  50.     public void drawVertical(Canvas c, RecyclerView parent) {  
  51.         final int childCount = parent.getChildCount();  
  52.         for (int i = 0; i < childCount; i++) {  
  53.             final View child = parent.getChildAt(i);  
  54.   
  55.             final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child  
  56.                     .getLayoutParams();  
  57.             final int top = child.getTop() - params.topMargin;  
  58.             final int bottom = child.getBottom() + params.bottomMargin;  
  59.             final int left = child.getRight() + params.rightMargin;  
  60.             final int right = left + mDivider.getIntrinsicWidth();  
  61.   
  62.             mDivider.setBounds(left, top, right, bottom);  
  63.             mDivider.draw(c);  
  64.         }  
  65.     }  
  66.   
  67.     private boolean isLastColum(RecyclerView parent, int pos, int spanCount,  
  68.                                 int childCount) {  
  69.         RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();  
  70.         if (layoutManager instanceof GridLayoutManager) {  
  71.             if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边  
  72.             {  
  73.                 return true;  
  74.             }  
  75.         } else if (layoutManager instanceof StaggeredGridLayoutManager) {  
  76.             int orientation = ((StaggeredGridLayoutManager) layoutManager)  
  77.                     .getOrientation();  
  78.             if (orientation == StaggeredGridLayoutManager.VERTICAL) {  
  79.                 if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边  
  80.                 {  
  81.                     return true;  
  82.                 }  
  83.             } else {  
  84.                 childCount = childCount - childCount % spanCount;  
  85.                 if (pos >= childCount)// 如果是最后一列,则不需要绘制右边  
  86.                     return true;  
  87.             }  
  88.         }  
  89.         return false;  
  90.     }  
  91.   
  92.     private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,  
  93.                               int childCount) {  
  94.         RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();  
  95.         if (layoutManager instanceof GridLayoutManager) {  
  96.             childCount = childCount - childCount % spanCount;  
  97.             if (pos >= childCount)// 如果是最后一行,则不需要绘制底部  
  98.                 return true;  
  99.         } else if (layoutManager instanceof StaggeredGridLayoutManager) {  
  100.             int orientation = ((StaggeredGridLayoutManager) layoutManager)  
  101.                     .getOrientation();  
  102.             // StaggeredGridLayoutManager 且纵向滚动  
  103.             if (orientation == StaggeredGridLayoutManager.VERTICAL) {  
  104.                 childCount = childCount - childCount % spanCount;  
  105.                 // 如果是最后一行,则不需要绘制底部  
  106.                 if (pos >= childCount)  
  107.                     return true;  
  108.             } else  
  109.             // StaggeredGridLayoutManager 且横向滚动  
  110.             {  
  111.                 // 如果是最后一行,则不需要绘制底部  
  112.                 if ((pos + 1) % spanCount == 0) {  
  113.                     return true;  
  114.                 }  
  115.             }  
  116.         }  
  117.         return false;  
  118.     }  
  119.   
  120.     @Override  
  121.     public void getItemOffsets(Rect outRect, int itemPosition,  
  122.                                RecyclerView parent) {  
  123.         int spanCount = getSpanCount(parent);  
  124.         int childCount = parent.getAdapter().getItemCount();  
  125.         if (isLastRaw(parent, itemPosition, spanCount, childCount))// 如果是最后一行,则不需要绘制底部  
  126.         {  
  127.             outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);  
  128.         } else if (isLastColum(parent, itemPosition, spanCount, childCount))// 如果是最后一列,则不需要绘制右边  
  129.         {  
  130.             outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());  
  131.         } else {  
  132.             outRect.set(0, 0, mDivider.getIntrinsicWidth(),  
  133.                     mDivider.getIntrinsicHeight());  
  134.         }  
  135.     }  
  136. }  
public class SupportGridItemLine extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
    private Drawable mDivider;

    public SupportGridItemLine(Context context) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

        drawHorizontal(c, parent);
        drawVertical(c, parent);

    }

    private int getSpanCount(RecyclerView parent) {
        // 列数
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {

            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager)
                    .getSpanCount();
        }
        return spanCount;
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin
                    + mDivider.getIntrinsicWidth();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicWidth();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    private boolean isLastColum(RecyclerView parent, int pos, int spanCount,
                                int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
            {
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
                {
                    return true;
                }
            } else {
                childCount = childCount - childCount % spanCount;
                if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
                    return true;
            }
        }
        return false;
    }

    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
                              int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
                return true;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            // StaggeredGridLayoutManager 且纵向滚动
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                childCount = childCount - childCount % spanCount;
                // 如果是最后一行,则不需要绘制底部
                if (pos >= childCount)
                    return true;
            } else
            // StaggeredGridLayoutManager 且横向滚动
            {
                // 如果是最后一行,则不需要绘制底部
                if ((pos + 1) % spanCount == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition,
                               RecyclerView parent) {
        int spanCount = getSpanCount(parent);
        int childCount = parent.getAdapter().getItemCount();
        if (isLastRaw(parent, itemPosition, spanCount, childCount))// 如果是最后一行,则不需要绘制底部
        {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        } else if (isLastColum(parent, itemPosition, spanCount, childCount))// 如果是最后一列,则不需要绘制右边
        {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(),
                    mDivider.getIntrinsicHeight());
        }
    }
}


好了,最后我们在使用的时候,先对RecycleView初始化相关的属性参数:

[html] view plain copy
print ?
  1. WrappingGridLayoutManager manager = new WrappingGridLayoutManager(getActivity(), 2);//2表示2个单元格  
  2.        manager.setOrientation(GridLayoutManager.VERTICAL);  
  3.        manager.setSmoothScrollbarEnabled(true);  
  4.        recyclerView.setLayoutManager(manager);  
  5.        recyclerView.setHasFixedSize(true);  
  6.        recyclerView.setNestedScrollingEnabled(false);  
  7.        recyclerView.addItemDecoration(new SupportGridItemLine(getActivity()));  
 WrappingGridLayoutManager manager = new WrappingGridLayoutManager(getActivity(), 2);//2表示2个单元格
        manager.setOrientation(GridLayoutManager.VERTICAL);
        manager.setSmoothScrollbarEnabled(true);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.addItemDecoration(new SupportGridItemLine(getActivity()));

至于网上有人说的,在RecycleView外加一个布局,不知道什么原因,我这里还是没有解决。

[html] view plain copy
print ?
  1. <RelativeLayout  
  2.                android:layout_width="match_parent"  
  3.                android:layout_height="wrap_content"  
  4.                android:descendantFocusability="blocksDescendants">  
  5.                <android.support.v7.widget.RecyclerView  
  6.                    android:id="@+id/menuRv"  
  7.                    android:layout_width="match_parent"  
  8.                    android:layout_height="wrap_content"  
  9.                    android:layout_marginLeft="@dimen/margin_16"  
  10.                    android:layout_marginRight="@dimen/margin_16">  
  11.   
  12.                android.support.v7.widget.RecyclerView>  
  13.                RelativeLayout>  
 
                

                
                


好了,姑且记下,帮助后来人,有疑问请加群:278792776

你可能感兴趣的:(Android开发系列)