1、单条横线及横线左右间隔
LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getActivity()); linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL); linearLayoutManager2.setSmoothScrollbarEnabled(true); linearLayoutManager2.setAutoMeasureEnabled(true); rvArticle.setHasFixedSize(true); rvArticle.setLayoutManager(linearLayoutManager2); int spacingleftRight = getResources().getDimensionPixelSize(R.dimen.s12); int spacingBottom = getResources().getDimensionPixelSize(R.dimen.s1); SpacesItemColorDecoration decoration2 = new SpacesItemColorDecoration(spacingleftRight, spacingBottom, getResources().getColor(R.color.colorDivider)); rvArticle.addItemDecoration(decoration2); rvArticle.setNestedScrollingEnabled(false); mArticleAdapter = new MainArticleRecyclerAdapter(getActivity(), articleList); rvArticle.setAdapter(mArticleAdapter);
/** * Created by Administrator on 2017/10/16. * int leftRight = dip2px(2); int topBottom = dip2px(2); rv_content.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom,getResources().getColor(R.color.colorPrimary))); 链接:http://www.jianshu.com/p/3b860938e503 */ public class SpacesItemColorDecoration extends RecyclerView.ItemDecoration { private int leftRight; private int topBottom; //color的传入方式是resouce.getcolor protected Drawable mDivider; public SpacesItemColorDecoration(int leftRight, int topBottom, int mColor) { this.leftRight = leftRight; this.topBottom = topBottom; if (mColor != 0) { mDivider = new ColorDrawable(mColor); } } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager(); //没有子view或者没有没有颜色直接return if (mDivider == null || layoutManager.getChildCount() == 0) { return; } int left; int right; int top; int bottom; final int childCount = parent.getChildCount(); if (layoutManager.getOrientation() == GridLayoutManager.VERTICAL) { for (int i = 0; i < childCount - 1; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); //将有颜色的分割线处于中间位置 float center = (layoutManager.getTopDecorationHeight(child) - topBottom) / 2; //计算下边的 left = layoutManager.getLeftDecorationWidth(child); right = parent.getWidth() - layoutManager.getLeftDecorationWidth(child); top = (int) (child.getBottom() + params.bottomMargin + center); bottom = top + topBottom; mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } else { for (int i = 0; i < childCount - 1; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); //将有颜色的分割线处于中间位置 float center = (layoutManager.getLeftDecorationWidth(child) - leftRight) / 2; //计算右边的 left = (int) (child.getRight() + params.rightMargin + center); right = left + leftRight; top = layoutManager.getTopDecorationHeight(child); bottom = parent.getHeight() - layoutManager.getTopDecorationHeight(child); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager(); //竖直方向的 if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) { //最后一项需要 bottom if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) { outRect.bottom = topBottom; } outRect.top = topBottom; outRect.left = leftRight; outRect.right = leftRight; } else { //最后一项需要right if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) { outRect.right = leftRight; } outRect.top = topBottom; outRect.left = leftRight; outRect.bottom = topBottom; } } }
2、多列分割
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2); recyclerViewTbk.setLayoutManager(gridLayoutManager); // recyclerViewTbk.addItemDecoration(new DividerGridItemDecoration(getActivity())); int spanCount = 2;//跟布局里面的spanCount属性是一致的 int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.s10);//每一个矩形的间距 boolean includeEdge = true;//如果设置成false那边缘地带就没有间距s //设置每个item间距 recyclerViewTbk.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacingInPixels, includeEdge)); mTbkAdapter = new MallTbkAdapter(getActivity(), tbkList); recyclerViewTbk.setAdapter(mTbkAdapter);
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; private int spacing; private boolean includeEdge; public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) { this.spanCount = spanCount; this.spacing = spacing; this.includeEdge = includeEdge; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { int position = parent.getChildAdapterPosition(view); // item position int column = position % spanCount; // item column if (includeEdge) { outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing) outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing) if (position < spanCount) { // top edge outRect.top = spacing; } outRect.bottom = spacing; // item bottom } else { outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing) outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing) if (position >= spanCount) { outRect.top = spacing; // item top } } } }
扫描二维码关注公众号,回复:
1144006 查看本文章