本文主要是介绍RecyclerView九宫格分隔线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
九宫格分隔线,无左右边与最下边线
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {private int spanColumnpan;private Drawable mDivider;// spanColumn 是 几列public GridDividerItemDecoration(Context context, int spanColumn) {mDivider = context.getResources().getDrawable(R.drawable.divider_recyclerview);this.spanColumnpan = spanColumn;}@Overridepublic void onDraw(Canvas c, RecyclerView parent) {drawHorizontal(c, parent);drawVertical(c, parent);}public void drawVertical(Canvas c, RecyclerView parent) {int left = parent.getPaddingLeft();int right = parent.getWidth() - parent.getPaddingRight();int childCount = parent.getChildCount();int top = 0;for (int i = 0; i < childCount; i += spanColumnpan) {View child = parent.getChildAt(i);RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();top = child.getBottom() + params.bottomMargin;int bottom = top + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}public void drawHorizontal(Canvas c, RecyclerView parent) {int top = parent.getPaddingTop();int childCount = parent.getChildCount();int left = 0;int bottom = 0;for (int i = 0; i < childCount; i = i + spanColumnpan) {View child = parent.getChildAt(i);RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();left = child.getRight() + params.rightMargin;bottom = child.getBottom() + params.bottomMargin;top = child.getTop() + params.topMargin;for (int j = 1; j <= spanColumnpan; j++) {int mLeft = left * j;int right = mLeft + mDivider.getIntrinsicHeight();mDivider.setBounds(mLeft, top, right, bottom);mDivider.draw(c);}}}@Overridepublic void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {}}
这篇关于RecyclerView九宫格分隔线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!