RecyclerView的萬能分割線

2021-08-31 06:55:01 字數 3448 閱讀 7495

//新增自定義分割線

recyclerviewdivider divider = new recyclerviewdivider ();

divider.setmargin(getactivity(), 72, 0 , 0, 0);

recyclerview.additemdecoration(divider);

import android.annotation.suppresslint;

import android.content.context;

import android.graphics.canvas;

import android.graphics.paint;

import android.graphics.rect;

import android.graphics.drawable.drawable;

import android.support.annotation.colorres;

import android.support.annotation.nonnull;

import android.support.v4.content.contextcompat;

import android.support.v4.view.viewcompat;

import android.support.v7.widget.recyclerview;

import android.view.view;

import android.widget.linearlayout;

/** * @author fz

* @date 2018/11/8

* describe:

*/public class recyclerviewdivider extends recyclerview.itemdecoration

public recyclerviewdivider(boolean isvertical) else

}public void setdividerheight(int px)

public void setdividerheight(context c, int dp)

public void setdrawable(@nonnull drawable drawable)

public void setcolor(int color)

public void setcolor(context c, @colorres int colorid)

public void setmargincolor(int color)

public void setmargincolor(context c, @colorres int colorid)

public void setmargin(int left, int top, int right, int bottom)

public void setmargin(context c, int leftdp, int topdp, int rightdp, int bottomdp)

@override

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

if (morientation == linearlayout.vertical) else

}/**

* 繪製縱向列表時的分隔線 這時分隔線是橫著的

* 每次 left相同,top根據child變化,right相同,bottom也變化

** @param canvas

* @param parent

*/@suppresslint("newapi")

private void drawvertical(canvas canvas, recyclerview parent) else

final int childcount = parent.getchildcount();

for (int i = 0; i < childcount - 1; i++)

if (null != mmarginbounds) else

mdivider.draw(canvas);

continue;

}if (null == mpaint)

//繪製普通分割線

if (null == mmarginbounds)

//繪製含間距分割線

if (null == mmarginpaint)

if (mmarginbounds.left > 0)

if (mmarginbounds.right > 0)

canvas.drawrect(left + mmarginbounds.left, top + mmarginbounds.top, right - mmarginbounds.right, bottom - mmarginbounds.bottom, mpaint);

}canvas.restore();

}/**

* 繪製橫向列表時的分隔線 這時分隔線是豎著的

* l、r 變化; t、b 不變

** @param canvas

* @param parent

*/private void drawhorizontal(canvas canvas, recyclerview parent) else

final int childcount = parent.getchildcount();

for (int i = 0; i < childcount - 1; i++)

if (null != mmarginbounds) else

mdivider.draw(canvas);

continue;

}if (null == mpaint)

//繪製普通分割線

if (null == mmarginbounds)

//繪製含間距分割線

if (null == mmarginpaint)

if (mmarginbounds.top > 0)

if (mmarginbounds.bottom > 0)

canvas.drawrect(left + mmarginbounds.left, top + mmarginbounds.top, right - mmarginbounds.right, bottom - mmarginbounds.bottom, mpaint);

}canvas.restore();

}//獲取分割線尺寸

@override

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

} else else }}

}

RecyclerView的萬能分割線

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

RecyclerView萬能的分割線

都知道recyclerview沒有自己的分割線,但是給了我們乙個新增分割線的方法。使用的時候呼叫該類如下 設定recyclerview的分割線 rcl.additemdecoration new recycleviewdivider secondactivity.this,linearlayoutm...

萬能介面卡實現 RecyclerView 的多布局

既然是萬能介面卡,那麼無論什麼介面卡,我們寫介面卡就因該有乙個固定的思想,具體實現是絕對不能寫在介面卡裡的,如果實現寫在了介面卡裡,那麼介面卡就不是萬能介面卡了 1.介面卡 public abstract class myadapter extends recyclerview.adapter 繫結...