RecyclerView的分割線

2021-08-20 08:27:36 字數 2057 閱讀 8131

public class recyclerviewdivider extends recyclerview.itemdecoration;

/****

預設分割線:高度為

2px,顏色為灰色

* 獲取屬性值,**

@param

context

* @param

orientation

列表方向

*/public recyclerviewdivider(context context, int orientation)

morientation = orientation;

final typedarray array = context.obtainstyledattributes(attrs);

mdivider = array.getdrawable(0);

array.recycle();

mdividerheight = mdivider.getintrinsicheight();

}/**

* 自定義分割線**

@param

context

* @param

orientation

列表方向

* @param

drawableid

分割線*/

public recyclerviewdivider(context context, int orientation, int drawableid)

morientation = orientation;

mdivider = contextcompat.getdrawable(context, drawableid);

mdividerheight = mdivider.getintrinsicheight();

}/**

* 自定義分割線**

@param

context

* @param

orientation

列表方向

* @param

dividerheight

分割線高度

* @param

dividercolor

分割線顏色

*/public recyclerviewdivider(context context, int orientation, int dividerheight, int dividercolor)

morientation = orientation;

mdividerheight = dividerheight;

mpaint = new paint(paint.anti_alias_flag);

mpaint.setcolor(dividercolor);

mpaint.setstyle(paint.style.fill);}//

獲取分割線尺寸

@override

public void getitemoffsets(rect outrect, view view, recyclerview parent, recyclerview.state state) else

outrect.set(0, 0, 0, mdividerheight);

}@override

public void ondraw(canvas c, recyclerview parent, recyclerview.state state) else}//

為橫方向

item,

畫分割線

private void drawhorizontalline(canvas canvas, recyclerview parent)

if (mpaint != null) }}

//為豎方向

item,

畫分割線

private void drawverticalline(canvas canvas, recyclerview parent)

if (mpaint != null) }}

}

詳解RecyclerView的分割線

1.新增recyclerview分割線,繼承自recyclerview.itemdecoration 可實現3個方法 1.方法執行的順序為 getitemoffsets 執行4次 ondraw 執行1次 child view ondraw 執行1次 ondrawover 執行1次 通過log日誌可以...

RecyclerVieW自定義華麗的分割線

recyclerview因沒有自帶分割線所以需要我們自定義分割線,在這裡提供了乙個簡單的實現分割線的方法 如下 public class itemdecoration extends recyclerview.itemdecoration override public void getitemof...

RecyclerView的萬能分割線

預設分割線 高度為2px,顏色為灰色 param context param orientation 列表方向 public recycleviewdivider context context,int orientation morientation orientation final typed...