RecyclerView 分间线设置
来源:互联网 发布:qq空间免费刷人气软件 编辑:程序博客网 时间:2024/06/03 15:16
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 } } }}
阅读全文
0 0
- RecyclerView 分间线设置
- recyclerview设置布局样式
- 为RecyclerView设置emptyView
- RecyclerView设置间距
- RecyclerView以及设置divider
- RecyclerView设置onItemClickListener
- 设置RecyclerView的OnItemClickListener
- RecyclerView 边框线设置
- RecyclerView 设置adapter
- Android RecyClerView 设置间隔
- RecyclerView设置EmptyView
- recyclerview点击事件设置
- RecyclerView设置Item点击事件
- RecyclerView 设置 itemclickListener 和 longclicklintener
- RecyclerView设置点击监听器OnClickListener
- RecyclerView使用GridLayoutManager间距设置
- RecyclerView设置OnItemClickListener的方法
- 设置RecyclerView 移动到底部
- angularjs的发货,已发货切换
- Java 多线程初学
- 项目中报错汇总
- Java
- 聊聊MSS与MTU
- RecyclerView 分间线设置
- cast使用技巧
- 地址解析协议-ARP
- mysql按首字母排序
- 二进制与十进制转换
- MacOS内核扩展驱动开发
- mysql的sql执行计划详解(非常有用)
- kettle 从hive中读取数据并写入MongoDB
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)里面的坑之 同一个controller部分方法可以拦截,部分方法拦截不了