不断学习,做更好的自己!💪
视频号 | CSDN | 简书 |
---|---|---|
欢迎打开微信,关注我的视频号:KevinDev | 点我 | 点我 |
ItemDecoration
是 RecyclerView
的一个抽象静态内部类,负责装饰 Item
视图,例如添加间距、高亮或者分组边界等。
1. 效果图
2. 源码
public class DividerItemDecoration extends RecyclerView.ItemDecoration{private Context mContext;private Drawable mDivider;private int mOrientation;public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;//我们通过获取系统属性中的listDivider来添加,在系统中的AppTheme中设置public static final int[] ATRRS = new int[]{android.R.attr.listDivider};public DividerItemDecoration(Context context, int orientation) {this.mContext = context;final TypedArray ta = context.obtainStyledAttributes(ATRRS);this.mDivider = ta.getDrawable(0);ta.recycle();setOrientation(orientation);}//设置屏幕的方向public void setOrientation(int orientation){if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST){throw new IllegalArgumentException("invalid orientation");}mOrientation = orientation;}@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {if (mOrientation == HORIZONTAL_LIST){drawVerticalLine(c, parent, state);}else {drawHorizontalLine(c, parent, state);}}//画横线, 这里的parent其实是显示在屏幕显示的这部分public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state){int left = PaddingLeft();int right = Width() - PaddingRight();final int childCount = ChildCount();for (int i = 0; i < childCount; i++){final View child = ChildAt(i);//获得child的布局信息final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams();final int top = Bottom() + params.bottomMargin;final int bottom = top + IntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);//Log.d("wnw", left + " " + top + " "+right+" "+bottom+" "+i);}}//画竖线public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state){int top = PaddingTop();int bottom = Height() - PaddingBottom();final int childCount = ChildCount();for (int i = 0; i < childCount; i++){final View child = ChildAt(i);//获得child的布局信息final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams();final int left = Right() + params.rightMargin;final int right = left + IntrinsicWidth();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}//由于Divider也有长宽高,每一个Item需要向下或者向右偏移@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {if(mOrientation == HORIZONTAL_LIST){//画横线,就是往下偏移一个分割线的高度outRect.set(0, 0, 0, IntrinsicHeight());}else {//画竖线,就是往右偏移一个分割线的宽度outRect.set(0, 0, IntrinsicWidth(), 0);}}
}
3. 使用方法
LinearLayoutManager manager = new LinearLayoutManager(this);mRecyclerView.setLayoutManager(manager);mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));mRecyclerView.setAdapter(mAdapter);
1. 效果图
2. 源码
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {private Context mContext;private int mFirstAndLastColumnW; //您所需指定的间隔宽度,主要为第一列和最后一列与父控件的间隔;行间距,列间距将动态分配private int mFirstRowTopMargin = 0; //第一行顶部是否需要间隔private int mLastRowBottomMargin = 0;private int mSpanCount = 0;private int mScreenW = 0;private int mItemDistance;/*** @param firstAndLastColumnW 间隔宽度* @param firstRowTopMargin 第一行顶部是否需要间隔*/public GridDividerItemDecoration(Context context, int firstAndLastColumnW, int firstRowTopMargin, int lastRowBottomMargin) {mContext = context;mFirstAndLastColumnW = firstAndLastColumnW;mFirstRowTopMargin = firstRowTopMargin;mLastRowBottomMargin = lastRowBottomMargin;}public void setFirstRowTopMargin(int firstRowTopMargin) {mFirstRowTopMargin = firstRowTopMargin;}public void setLastRowBottomMargin(int lastRowBottomMargin) {mLastRowBottomMargin = lastRowBottomMargin;}public void setItemDistance(int itemDistance) {mItemDistance = itemDistance;}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {ItemOffsets(outRect, view, parent, state);int top = 0;int left;int right;int bottom;int itemPosition = ((RecyclerView.LayoutParams) LayoutParams()).getViewLayoutPosition();mSpanCount = getSpanCount(parent);int childCount = Adapter().getItemCount();// 屏幕宽度-View的宽度*spanCount 得到屏幕剩余空间int maxDividerWidth = getMaxDividerWidth(view);int spaceWidth = mFirstAndLastColumnW;//首尾两列与父布局之间的间隔// 除去首尾两列,item与item之间的距离int eachItemWidth = maxDividerWidth / mSpanCount;int dividerItemWidth = (maxDividerWidth - 2 * spaceWidth) / (mSpanCount - 1);//item与item之间的距离left = itemPosition % mSpanCount * (dividerItemWidth - eachItemWidth) + spaceWidth;right = eachItemWidth - left;bottom = dividerItemWidth;// 首行if (mFirstRowTopMargin > 0 && isFirstRow(parent, itemPosition, mSpanCount, childCount)) {top = mFirstRowTopMargin;}//最后一行if (isLastRow(parent, itemPosition, mSpanCount, childCount)) {if (mLastRowBottomMargin < 0) {bottom = 0;} else {bottom = mLastRowBottomMargin;}}outRect.set(left, top, right, bottom);}/*** 获取Item View的大小,若无则自动分配空间* 并根据 屏ge幕宽度-View的宽度*spanCount 得到屏幕剩余空间** @param view* @return*/private int getMaxDividerWidth(View view) {int itemWidth = LayoutParams().width;int itemHeight = LayoutParams().height;int screenWidth = getScreenWidth();int maxDividerWidth = screenWidth - itemWidth * mSpanCount;if (itemHeight < 0 || itemWidth < 0 || maxDividerWidth <= (mSpanCount - 1) * mFirstAndLastColumnW) {LayoutParams().width = getAttachCloumnWidth();LayoutParams().height = getAttachCloumnWidth();maxDividerWidth = screenWidth - LayoutParams().width * mSpanCount;}return maxDividerWidth;}private int getScreenWidth() {if (mScreenW > 0) {return mScreenW;}mScreenW = Resources().getDisplayMetrics().widthPixels > Resources().getDisplayMetrics().heightPixels? Resources().getDisplayMetrics().heightPixels : Resources().getDisplayMetrics().widthPixels;return mScreenW;}/*** 根据屏幕宽度和item数量分配 item View的width和height** @return*/private int getAttachCloumnWidth() {int itemWidth = 0;int spaceWidth = 0;try {int width = getScreenWidth();spaceWidth = 2 * mFirstAndLastColumnW;itemWidth = (width - spaceWidth) / mSpanCount - 40;} catch (Exception e) {e.printStackTrace();}return itemWidth;}/*** 判读是否是第一列** @param parent* @param pos* @param spanCount* @return*/private boolean isFirstColumn(RecyclerView parent, int pos, int spanCount) {RecyclerView.LayoutManager layoutManager = LayoutManager();if (layoutManager instanceof GridLayoutManager) {if (pos % spanCount == 0) {return true;}} else if (layoutManager instanceof StaggeredGridLayoutManager) {int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();if (orientation == StaggeredGridLayoutManager.VERTICAL) {if (pos % spanCount == 0) {// 第一列return true;}} else {}}return false;}/*** 判断是否是最后一列** @param parent* @param pos* @param spanCount* @return*/private boolean isLastColumn(RecyclerView parent, int pos, int spanCount) {RecyclerView.LayoutManager layoutManager = LayoutManager();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 {}}return false;}/*** 判读是否是最后一行** @param parent* @param pos* @param spanCount* @param childCount* @return*/private boolean isLastRow(RecyclerView parent, int pos, int spanCount, int childCount) {RecyclerView.LayoutManager layoutManager = LayoutManager();if (layoutManager instanceof GridLayoutManager) {int lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1;return lines == pos / spanCount + 1;}return false;}/*** 判断是否是第一行** @param parent* @param pos* @param spanCount* @param childCount* @return*/private boolean isFirstRow(RecyclerView parent, int pos, int spanCount, int childCount) {RecyclerView.LayoutManager layoutManager = LayoutManager();if (layoutManager instanceof GridLayoutManager) {if ((pos / spanCount + 1) == 1) {return true;} else {return false;}}return false;}/*** 获取列数** @param parent* @return*/private int getSpanCount(RecyclerView parent) {int spanCount = -1;RecyclerView.LayoutManager layoutManager = LayoutManager();if (layoutManager instanceof GridLayoutManager) {spanCount = ((GridLayoutManager) layoutManager).getSpanCount();} else if (layoutManager instanceof StaggeredGridLayoutManager) {spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();}return spanCount;}
}
3. 使用方法
int firstAndLastColumnW = DensityUtil.dip2px(this, 20);int firstRowTopMargin = DensityUtil.dip2px(this, 20);GridDividerItemDecoration gridDividerItemDecoration =new GridDividerItemDecoration(this, firstAndLastColumnW, firstRowTopMargin, firstRowTopMargin);gridDividerItemDecoration.setFirstRowTopMargin(firstRowTopMargin);gridDividerItemDecoration.setLastRowBottomMargin(firstRowTopMargin);mRecyclerView.addItemDecoration(gridDividerItemDecoration);GridLayoutManager layoutManager = new GridLayoutManager(this, 3);mRecyclerView.setLayoutManager(layoutManager);mRecyclerView.setAdapter(mAdapter);
本文发布于:2024-01-31 21:50:49,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170670905231596.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |