recyclerview通用的分隔條

public class BaseMultiItemDecorationextends RecyclerView.ItemDecoration {

private int mRowSpace, mColumnSpace;

? ? @Override

? ? public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

int position = parent.getChildAdapterPosition(view);

? ? ? ? if (parent.getAdapter() !=null) {

RecyclerView.Adapter parentAdapter = parent.getAdapter();

? ? ? ? ? ? int type = parentAdapter.getItemViewType(position);

? ? ? ? ? ? RecyclerView.LayoutManager manager = parent.getLayoutManager();

? ? ? ? ? ? if (managerinstanceof GridLayoutManager) {

GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;

? ? ? ? ? ? ? ? int count = parentAdapter.getItemCount();

? ? ? ? ? ? ? ? int spanCount = gridLayoutManager.getSpanCount();

? ? ? ? ? ? ? ? if (LinearLayoutManager.VERTICAL == gridLayoutManager.getOrientation()) {

if (spanCount ==1) {

getLinearVerticalOffsets(outRect, parent, type, position);

? ? ? ? ? ? ? ? ? ? }else {

getGridVerticalOffsets(gridLayoutManager,outRect, parent, type, position, spanCount, count);

? ? ? ? ? ? ? ? ? ? }

}else if (LinearLayoutManager.HORIZONTAL == gridLayoutManager.getOrientation()) {

if (spanCount ==1) {

getLinearHorizontalOffsets(outRect, parent, type, position);

? ? ? ? ? ? ? ? ? ? }else {

getGridHorizontalOffsets(gridLayoutManager,outRect, parent, type, position, spanCount, count);

? ? ? ? ? ? ? ? ? ? }

}

}else if (managerinstanceof LinearLayoutManager) {

LinearLayoutManager linearLayoutManager = (LinearLayoutManager) manager;

? ? ? ? ? ? ? ? if (LinearLayoutManager.VERTICAL == linearLayoutManager.getOrientation()) {

getLinearVerticalOffsets(outRect, parent, type, position);

? ? ? ? ? ? ? ? }else {

getLinearHorizontalOffsets(outRect, parent, type, position);

? ? ? ? ? ? ? ? }

}

}

}

private void getLinearHorizontalOffsets(@NonNull Rect outRect, @NonNull RecyclerView parent, int type, int position) {

if (position ==0) {

outRect.left =mColumnSpace;

? ? ? ? }

if (type <0) {

return;

? ? ? ? }

outRect.bottom =mRowSpace;

? ? ? ? outRect.top =mRowSpace;

? ? ? ? outRect.right =mColumnSpace;

? ? }

private void getLinearVerticalOffsets(@NonNull Rect outRect, @NonNull RecyclerView parent, int type, int position) {

if (position ==0) {

outRect.top =mRowSpace;

? ? ? ? }

if (type <0) {

return;

? ? ? ? }

outRect.bottom =mRowSpace;

? ? ? ? outRect.left =mColumnSpace;

? ? ? ? outRect.right =mColumnSpace;

? ? }

private void getGridHorizontalOffsets(GridLayoutManager gridLayoutManager, @NonNull Rect outRect, @NonNull RecyclerView parent, int type, int position, int spanCount, int count) {

GridLayoutManager.SpanSizeLookup sizeLookup = gridLayoutManager.getSpanSizeLookup();

? ? ? ? if(sizeLookup==null){

return;

? ? ? ? }

final int groupIndex = sizeLookup.getSpanGroupIndex(position, spanCount);

? ? ? ? int index = sizeLookup.getSpanIndex(position, spanCount);

? ? ? ? int spanSize = sizeLookup.getSpanSize(position);

? ? ? ? if(groupIndex==0){

outRect.left =mColumnSpace;

? ? ? ? }

if (type <0) {

return;

? ? ? ? }

outRect.right=mColumnSpace;

? ? ? ? int column=(index+spanSize)%spanCount;

? ? ? ? outRect.top = index==0?mRowSpace:mRowSpace/2;

? ? ? ? outRect.bottom=column==0?mRowSpace:mRowSpace/2;

? ? }

private void getGridVerticalOffsets(GridLayoutManager gridLayoutManager, @NonNull Rect outRect, @NonNull RecyclerView parent, int type, int position, int spanCount, int count) {

GridLayoutManager.SpanSizeLookup sizeLookup = gridLayoutManager.getSpanSizeLookup();

? ? ? ? if(sizeLookup==null){

return;

? ? ? ? }

final int groupIndex = sizeLookup.getSpanGroupIndex(position, spanCount);

? ? ? ? int index = sizeLookup.getSpanIndex(position, spanCount);

? ? ? ? int spanSize = sizeLookup.getSpanSize(position);

? ? ? ? if(groupIndex==0){

outRect.top =mRowSpace;

? ? ? ? }

if (type <0) {

return;

? ? ? ? }

outRect.bottom =mRowSpace;

? ? ? ? int column=(index+spanSize)%spanCount;

? ? ? ? outRect.left = index==0?mColumnSpace:mColumnSpace/2;

? ? ? ? outRect.right=column==0?mColumnSpace:mColumnSpace/2;

? ? }

public BaseMultiItemDecoration(int space) {

this.mColumnSpace =mRowSpace = space;

? ? }

public BaseMultiItemDecoration(int rowSpace, int columnSpace) {

this.mRowSpace = rowSpace;

? ? ? ? this.mColumnSpace = columnSpace;

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末生逸,一起剝皮案震驚了整個濱河市眼滤,隨后出現(xiàn)的幾起案子拭宁,更是在濱河造成了極大的恐慌轴合,老刑警劉巖轴总,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件白粉,死亡現(xiàn)場離奇詭異昔穴,居然都是意外死亡暖侨,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門趁窃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來牧挣,“玉大人,你說我怎么就攤上這事棚菊〗龋” “怎么了叔汁?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵统求,是天一觀的道長。 經(jīng)常有香客問我据块,道長码邻,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任另假,我火速辦了婚禮像屋,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘边篮。我一直安慰自己己莺,他們只是感情好,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布戈轿。 她就那樣靜靜地躺著凌受,像睡著了一般。 火紅的嫁衣襯著肌膚如雪思杯。 梳的紋絲不亂的頭發(fā)上胜蛉,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天,我揣著相機與錄音,去河邊找鬼誊册。 笑死领突,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的案怯。 我是一名探鬼主播君旦,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼嘲碱!你這毒婦竟也來了于宙?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤悍汛,失蹤者是張志新(化名)和其女友劉穎捞魁,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體离咐,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡谱俭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了宵蛀。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片昆著。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖术陶,靈堂內(nèi)的尸體忽然破棺而出凑懂,到底是詐尸還是另有隱情,我是刑警寧澤梧宫,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布接谨,位于F島的核電站,受9級特大地震影響塘匣,放射性物質(zhì)發(fā)生泄漏脓豪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一忌卤、第九天 我趴在偏房一處隱蔽的房頂上張望扫夜。 院中可真熱鬧,春花似錦驰徊、人聲如沸笤闯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽颗味。三九已至,卻和暖如春勋桶,著一層夾襖步出監(jiān)牢的瞬間脱衙,已是汗流浹背侥猬。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留捐韩,地道東北人退唠。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像荤胁,于是被迫代替她去往敵國和親瞧预。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容