From 4654bdd8ec299362686ace7df33756433505bd43 Mon Sep 17 00:00:00 2001 From: jingbin <770413277@qq.com> Date: Fri, 3 Jan 2020 15:33:20 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E7=BA=BF=E6=80=A7=E5=B8=83=E5=B1=80?= =?UTF-8?q?=E5=88=86=E5=89=B2=E7=BA=BF=E5=A2=9E=E5=8A=A0=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=A2=9C=E8=89=B2=E9=AB=98=E5=BA=A6=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../decoration/SpacesItemDecoration.java | 126 ++++++++++++++---- .../activity/HeaderFooterActivity.java | 4 +- .../fragment/LinearFragment.java | 3 +- .../main/res/drawable/shape_line_custom.xml | 19 +++ 4 files changed, 123 insertions(+), 29 deletions(-) create mode 100644 app/src/main/res/drawable/shape_line_custom.xml diff --git a/ByRecyclerview/src/main/java/me/jingbin/library/decoration/SpacesItemDecoration.java b/ByRecyclerview/src/main/java/me/jingbin/library/decoration/SpacesItemDecoration.java index 456b8ac..639a2fc 100644 --- a/ByRecyclerview/src/main/java/me/jingbin/library/decoration/SpacesItemDecoration.java +++ b/ByRecyclerview/src/main/java/me/jingbin/library/decoration/SpacesItemDecoration.java @@ -19,9 +19,9 @@ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; +import android.graphics.Paint; import android.graphics.Rect; import android.graphics.drawable.Drawable; -import android.util.Log; import android.view.View; import android.widget.LinearLayout; @@ -59,7 +59,26 @@ public class SpacesItemDecoration extends RecyclerView.ItemDecoration { * Current orientation. Either {@link #HORIZONTAL} or {@link #VERTICAL}. */ private int mOrientation; + private Paint mPaint; + /** + * 如果是横向 - 宽度 + * 如果是纵向 - 高度 + */ + private int mDividerSpacing; + /** + * 如果是横向 - 左边距 + * 如果是纵向 - 上边距 + */ + private int mLeftTopPadding; + /** + * 如果是横向 - 右边距 + * 如果是纵向 - 下边距 + */ + private int mRightBottomPadding; + public SpacesItemDecoration(Context context) { + this(context, VERTICAL, 0, 1); + } public SpacesItemDecoration(Context context, int orientation) { this(context, orientation, 0, 1); @@ -85,9 +104,6 @@ public SpacesItemDecoration(Context context, int orientation, int headerNoShowSi setOrientation(orientation); final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = a.getDrawable(0); - if (mDivider == null) { - Log.w(TAG, "@android:attr/listDivider was not set in the theme used for this " + "SpacesItemDecoration. Please set that attribute all call setDrawable()"); - } a.recycle(); } @@ -97,11 +113,12 @@ public SpacesItemDecoration(Context context, int orientation, int headerNoShowSi * * @param orientation {@link #HORIZONTAL} or {@link #VERTICAL} */ - public void setOrientation(int orientation) { + public SpacesItemDecoration setOrientation(int orientation) { if (orientation != HORIZONTAL && orientation != VERTICAL) { throw new IllegalArgumentException("Invalid orientation. It should be either HORIZONTAL or VERTICAL"); } mOrientation = orientation; + return this; } /** @@ -109,20 +126,22 @@ public void setOrientation(int orientation) { * * @param drawable Drawable that should be used as a divider. */ - public void setDrawable(Drawable drawable) { + public SpacesItemDecoration setDrawable(Drawable drawable) { if (drawable == null) { throw new IllegalArgumentException("drawable cannot be null."); } mDivider = drawable; + return this; } - public void setDrawable(@DrawableRes int id) { + public SpacesItemDecoration setDrawable(@DrawableRes int id) { setDrawable(ContextCompat.getDrawable(mContext, id)); + return this; } @Override public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) { - if (parent.getLayoutManager() == null || mDivider == null) { + if (parent.getLayoutManager() == null || (mDivider == null && mPaint == null)) { return; } if (mOrientation == VERTICAL) { @@ -140,8 +159,7 @@ private void drawVertical(Canvas canvas, RecyclerView parent, RecyclerView.State if (parent.getClipToPadding()) { left = parent.getPaddingLeft(); right = parent.getWidth() - parent.getPaddingRight(); - canvas.clipRect(left, parent.getPaddingTop(), right, - parent.getHeight() - parent.getPaddingBottom()); + canvas.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom()); } else { left = 0; right = parent.getWidth(); @@ -159,11 +177,22 @@ private void drawVertical(Canvas canvas, RecyclerView parent, RecyclerView.State } // 过滤到尾部不显示的分割线 if (childRealPosition <= lastPosition - mFooterNoShowSize) { - parent.getDecoratedBoundsWithMargins(child, mBounds); - final int bottom = mBounds.bottom + Math.round(child.getTranslationY()); - final int top = bottom - mDivider.getIntrinsicHeight(); - mDivider.setBounds(left, top, right, bottom); - mDivider.draw(canvas); + if (mDivider != null) { + parent.getDecoratedBoundsWithMargins(child, mBounds); + final int bottom = mBounds.bottom + Math.round(child.getTranslationY()); + final int top = bottom - mDivider.getIntrinsicHeight(); + mDivider.setBounds(left, top, right, bottom); + mDivider.draw(canvas); + } + + if (mPaint != null) { + RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); + int left1 = left + mLeftTopPadding; + int right1 = right - mRightBottomPadding; + int top1 = child.getBottom() + params.bottomMargin; + int bottom1 = top1 + mDividerSpacing; + canvas.drawRect(left1, top1, right1, bottom1, mPaint); + } } } canvas.restore(); @@ -196,11 +225,22 @@ private void drawHorizontal(Canvas canvas, RecyclerView parent, RecyclerView.Sta } // 过滤到尾部不显示的分割线 if (childRealPosition <= lastPosition - mFooterNoShowSize) { - parent.getDecoratedBoundsWithMargins(child, mBounds); - final int right = mBounds.right + Math.round(child.getTranslationX()); - final int left = right - mDivider.getIntrinsicWidth(); - mDivider.setBounds(left, top, right, bottom); - mDivider.draw(canvas); + if (mDivider != null) { + parent.getDecoratedBoundsWithMargins(child, mBounds); + final int right = mBounds.right + Math.round(child.getTranslationX()); + final int left = right - mDivider.getIntrinsicWidth(); + mDivider.setBounds(left, top, right, bottom); + mDivider.draw(canvas); + } + + if (mPaint != null) { + RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); + int left1 = child.getRight() + params.rightMargin; + int right1 = left1 + mDividerSpacing; + int top1 = top + mLeftTopPadding; + int bottom1 = bottom - mRightBottomPadding; + canvas.drawRect(left1, top1, right1, bottom1, mPaint); + } } } canvas.restore(); @@ -208,7 +248,7 @@ private void drawHorizontal(Canvas canvas, RecyclerView parent, RecyclerView.Sta @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { - if (mDivider == null) { + if (mDivider == null && mPaint == null) { outRect.set(0, 0, 0, 0); return; } @@ -217,7 +257,7 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle int lastPosition = state.getItemCount() - 1; int position = parent.getChildAdapterPosition(view); if (position <= lastPosition - mFooterNoShowSize) { - outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); + outRect.set(0, 0, 0, mDivider != null ? mDivider.getIntrinsicHeight() : mDividerSpacing); } else { outRect.set(0, 0, 0, 0); } @@ -225,7 +265,7 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle int lastPosition = state.getItemCount() - 1; int position = parent.getChildAdapterPosition(view); if (position <= lastPosition - mFooterNoShowSize) { - outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); + outRect.set(0, 0, mDivider != null ? mDivider.getIntrinsicWidth() : mDividerSpacing, 0); } else { outRect.set(0, 0, 0, 0); } @@ -238,9 +278,10 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle * @param headerNoShowSize 头部 不显示分割线的item个数 * @param footerNoShowSize 尾部 不显示分割线的item个数,默认1,不显示最后一个,最后一个一般为加载更多view */ - public void setNoShowDivider(int headerNoShowSize, int footerNoShowSize) { + public SpacesItemDecoration setNoShowDivider(int headerNoShowSize, int footerNoShowSize) { this.mHeaderNoShowSize = headerNoShowSize; this.mFooterNoShowSize = footerNoShowSize; + return this; } /** @@ -248,8 +289,43 @@ public void setNoShowDivider(int headerNoShowSize, int footerNoShowSize) { * * @param headerNoShowSize 头部 不显示分割线的item个数 */ - public void setHeaderNoShowDivider(int headerNoShowSize) { + public SpacesItemDecoration setHeaderNoShowDivider(int headerNoShowSize) { this.mHeaderNoShowSize = headerNoShowSize; + return this; + } + + public SpacesItemDecoration setParam(int dividerColor, int dividerSpacing) { + return setParam(dividerColor, dividerSpacing, 0, 0); + } + + /** + * 直接设置分割线颜色等,不设置drawable + * + * @param dividerColor 分割线颜色 + * @param dividerSpacing 分割线间距 + * @param leftTopPaddingDp 如果是横向 - 左边距 + * 如果是纵向 - 上边距 + * @param rightBottomPaddingDp 如果是横向 - 右边距 + * 如果是纵向 - 下边距 + */ + public SpacesItemDecoration setParam(int dividerColor, int dividerSpacing, float leftTopPaddingDp, float rightBottomPaddingDp) { + mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mPaint.setStyle(Paint.Style.FILL); + mPaint.setColor(ContextCompat.getColor(mContext, dividerColor)); + mDividerSpacing = dividerSpacing; + mLeftTopPadding = dip2px(leftTopPaddingDp); + mRightBottomPadding = dip2px(rightBottomPaddingDp); + mDivider = null; + return this; } + /** + * 根据手机的分辨率从 dp 的单位 转成为 px(像素) + */ + public int dip2px(float dpValue) { + final float scale = mContext.getResources().getDisplayMetrics().density; + return (int) (dpValue * scale + 0.5f); + } + + } diff --git a/app/src/main/java/me/jingbin/byrecyclerview/activity/HeaderFooterActivity.java b/app/src/main/java/me/jingbin/byrecyclerview/activity/HeaderFooterActivity.java index fd2b357..af04128 100644 --- a/app/src/main/java/me/jingbin/byrecyclerview/activity/HeaderFooterActivity.java +++ b/app/src/main/java/me/jingbin/byrecyclerview/activity/HeaderFooterActivity.java @@ -52,9 +52,7 @@ private void initAdapter() { LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(RecyclerView.VERTICAL); binding.recyclerView.setLayoutManager(layoutManager); - SpacesItemDecoration itemDecoration = new SpacesItemDecoration(this, SpacesItemDecoration.VERTICAL, 1); - itemDecoration.setDrawable(ContextCompat.getDrawable(binding.recyclerView.getContext(), R.drawable.shape_line)); - binding.recyclerView.addItemDecoration(itemDecoration); + binding.recyclerView.addItemDecoration(new SpacesItemDecoration(this, SpacesItemDecoration.VERTICAL).setDrawable(R.drawable.shape_line)); binding.recyclerView.setAdapter(mAdapter); binding.recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() { @Override diff --git a/app/src/main/java/me/jingbin/byrecyclerview/fragment/LinearFragment.java b/app/src/main/java/me/jingbin/byrecyclerview/fragment/LinearFragment.java index 64f0e03..b6027a0 100644 --- a/app/src/main/java/me/jingbin/byrecyclerview/fragment/LinearFragment.java +++ b/app/src/main/java/me/jingbin/byrecyclerview/fragment/LinearFragment.java @@ -81,7 +81,8 @@ private void initAdapter() { layoutManager.setOrientation(RecyclerView.VERTICAL); recyclerView.setLayoutManager(layoutManager); // 加了分割线,滚动条才会置顶 - recyclerView.addItemDecoration(new SpacesItemDecoration(recyclerView.getContext(), SpacesItemDecoration.VERTICAL, 1)); + SpacesItemDecoration itemDecoration = new SpacesItemDecoration(recyclerView.getContext(), SpacesItemDecoration.VERTICAL, 1); + recyclerView.addItemDecoration(itemDecoration.setParam(R.color.colorBlue, 10, 10, 10)); recyclerView.setAdapter(mAdapter); recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() { @Override diff --git a/app/src/main/res/drawable/shape_line_custom.xml b/app/src/main/res/drawable/shape_line_custom.xml new file mode 100644 index 0000000..79ce7af --- /dev/null +++ b/app/src/main/res/drawable/shape_line_custom.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + \ No newline at end of file