RecyclerView分割线的技巧
生活随笔
收集整理的這篇文章主要介紹了
RecyclerView分割线的技巧
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
RecyclerView分割線的技巧
真的很簡單,因為方法別人都已經寫好了,不多說了還是看源碼:
package com.xiayiye.yhsh.recyclerviewdemo;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.support.v4.content.ContextCompat; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View;/*** 創 建 者:下一頁5(輕飛揚)* 創建時間:2018/1/3.14:52* 個人小站:http://wap.yhsh.ai* 聯系作者:企鵝 13343401268* 博客地址:http://blog.csdn.net/xiayiye5* 類的包名:com.xiayiye.yhsh.recyclerviewdemo* 項目名稱: AndroidStudio3.0*/public class RecycleViewDivider extends RecyclerView.ItemDecoration {private Paint mPaint;private Drawable mDivider;private int mDividerHeight = 2;//分割線高度,默認為1pxprivate int mOrientation;//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTALprivate static final int[] ATTRS = new int[]{android.R.attr.listDivider};/*** 默認分割線:高度為2px,顏色為灰色** @param context* @param orientation 列表方向*/public RecycleViewDivider(Context context, int orientation) {if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {throw new IllegalArgumentException("請輸入正確的參數!");}mOrientation = orientation;final TypedArray a = context.obtainStyledAttributes(ATTRS);mDivider = a.getDrawable(0);a.recycle();}/*** 自定義分割線** @param context* @param orientation 列表方向* @param drawableId 分割線圖片*/public RecycleViewDivider(Context context, int orientation, int drawableId) {this(context, orientation);mDivider = ContextCompat.getDrawable(context, drawableId);mDividerHeight = mDivider.getIntrinsicHeight();}/*** 自定義分割線** @param context* @param orientation 列表方向* @param dividerHeight 分割線高度* @param dividerColor 分割線顏色*/public RecycleViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) {this(context, orientation);mDividerHeight = dividerHeight;mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);mPaint.setColor(dividerColor);mPaint.setStyle(Paint.Style.FILL);}//獲取分割線尺寸@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {super.getItemOffsets(outRect, view, parent, state);outRect.set(0, 0, 0, mDividerHeight);}//繪制分割線@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {super.onDraw(c, parent, state);if (mOrientation == LinearLayoutManager.VERTICAL) {drawVertical(c, parent);} else {drawHorizontal(c, parent);}}//繪制橫向 item 分割線private void drawHorizontal(Canvas canvas, RecyclerView parent) {final int top = parent.getPaddingTop();final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();final int childSize = parent.getChildCount();for (int i = 0; i < childSize; i++) {final View child = parent.getChildAt(i);RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();final int left = child.getRight() + layoutParams.rightMargin;final int right = left + mDividerHeight;if (mDivider != null) {mDivider.setBounds(left, top, right, bottom);mDivider.draw(canvas);}if (mPaint != null) {canvas.drawRect(left, top, right, bottom, mPaint);}}}//繪制縱向 item 分割線private void drawVertical(Canvas canvas, RecyclerView parent) {final int left = parent.getPaddingLeft();final int right = parent.getMeasuredWidth() - parent.getPaddingRight();final int childSize = parent.getChildCount();for (int i = 0; i < childSize; i++) {final View child = parent.getChildAt(i);RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();final int top = child.getBottom() + layoutParams.bottomMargin;final int bottom = top + mDividerHeight;if (mDivider != null) {mDivider.setBounds(left, top, right, bottom);mDivider.draw(canvas);}if (mPaint != null) {canvas.drawRect(left, top, right, bottom, mPaint);}}} }那么這么用呢?、很簡單,這里涉及到單個構造方法,根據下面的調用就好了
添加默認分割線:高度為2px,顏色為灰色
mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL));- 1
添加自定義分割線:可自定義分割線drawable
mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, R.drawable.divider_mileage));- 1
- 2
添加自定義分割線:可自定義分割線高度和顏色
mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 10, getResources().getColor(R.color.divide_gray_color))); 這個還是我看的別人的博客稍微改了下方向,非常感謝博主,因為原博客你會發現設置的方向弄反了
原地址博客地址
還是不懂得可以下載源碼查看源碼地址點擊即可下載
注意:
大家下載demo導入基本會報錯,因為我用的Android studio3.0最新版本寫的,不過沒事解決很簡單,修改下gradle版本即可,不懂得可咨詢我謝謝。
總結
以上是生活随笔為你收集整理的RecyclerView分割线的技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天猫超市华南中心仓启用!广州70%地区包
- 下一篇: 尼康:尼康Z 9预计将销售超过30万台