RecyclerView中的ItemDecoration(多功能)

package com.hjlm.sanfentianxia.view;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Rect;

import android.graphics.drawable.Drawable;

import android.view.View;

import com.hjlm.sanfentianxia.R;

import com.hjlm.sanfentianxia.util.MyUtil;

import androidx.annotation.NonNull;

import androidx.recyclerview.widget.GridLayoutManager;

import androidx.recyclerview.widget.RecyclerView;

import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class RecyclerDecorationextends RecyclerView.ItemDecoration {

private static final int[]ATTRS =new int[]{android.R.attr.listDivider};

? ? private DrawablemDivider;

? ? private int mWidth, headSize;

? ? private int color;

? ? public RecyclerDecoration(Context context) {

final TypedArray a = context.obtainStyledAttributes(ATTRS);

? ? ? ? mDivider = a.getDrawable(0);

? ? ? ? mWidth = MyUtil.dip2px(context, 10);

? ? ? ? color = context.getResources().getColor(R.color.color_grey_EEEEEE);

? ? ? ? a.recycle();

? ? }

//設(shè)置有幾個(gè)頭部(默認(rèn)有一個(gè))

? ? public RecyclerDecoration(Context context, int size) {

final TypedArray a = context.obtainStyledAttributes(ATTRS);

? ? ? ? mDivider = a.getDrawable(0);

? ? ? ? mWidth = MyUtil.dip2px(context, 10);

? ? ? ? color = context.getResources().getColor(R.color.color_grey_EEEEEE);

? ? ? ? headSize = size;

? ? ? ? a.recycle();

? ? }

//設(shè)置顏色和寬度

? ? public RecyclerDecoration(Context context, int mColor, int width) {

final TypedArray a = context.obtainStyledAttributes(ATTRS);

? ? ? ? mDivider = a.getDrawable(0);

? ? ? ? color = context.getResources().getColor(mColor);

? ? ? ? mWidth = MyUtil.dip2px(context, width);

? ? ? ? a.recycle();

? ? }

public RecyclerDecoration(Context context, int mColor, int width, int size) {

final TypedArray a = context.obtainStyledAttributes(ATTRS);

? ? ? ? mDivider = a.getDrawable(0);

? ? ? ? color = context.getResources().getColor(mColor);

? ? ? ? mWidth = MyUtil.dip2px(context, width);

? ? ? ? headSize = size;

? ? ? ? a.recycle();

? ? }

@Override

? ? public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

c.drawColor(color);

? ? ? ? drawHorizontal(c, parent);

? ? ? ? drawVertical(c, parent);

? ? }

private int getSpanCount(RecyclerView parent) {

// 列數(shù)

? ? ? ? int spanCount = -1;

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

? ? ? ? if (layoutManagerinstanceof GridLayoutManager) {

spanCount = ((GridLayoutManager) layoutManager).getSpanCount();

? ? ? ? }else if (layoutManagerinstanceof StaggeredGridLayoutManager) {

spanCount = ((StaggeredGridLayoutManager) layoutManager)

.getSpanCount();

? ? ? ? }

return spanCount;

? ? }

public void drawHorizontal(Canvas c, RecyclerView parent) {

int childCount = parent.getChildCount();

? ? ? ? for (int i =0; i < childCount; i++) {

final View child = parent.getChildAt(i);

? ? ? ? ? ? final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child

.getLayoutParams();

? ? ? ? ? ? final int left = child.getLeft() - params.leftMargin;

? ? ? ? ? ? final int right = child.getRight() + params.rightMargin

? ? ? ? ? ? ? ? ? ? +mDivider.getIntrinsicWidth();

? ? ? ? ? ? final int top = child.getBottom() + params.bottomMargin;

? ? ? ? ? ? final int bottom = top +mDivider.getIntrinsicHeight();

? ? ? ? ? ? mDivider.setBounds(left, top, right, bottom);

? ? ? ? ? ? mDivider.draw(c);

? ? ? ? }

}

public void drawVertical(Canvas c, RecyclerView parent) {

final int childCount = parent.getChildCount();

? ? ? ? for (int i =0; i < childCount; i++) {

final View child = parent.getChildAt(i);

? ? ? ? ? ? final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child

.getLayoutParams();

? ? ? ? ? ? final int top = child.getTop() - params.topMargin;

? ? ? ? ? ? final int bottom = child.getBottom() + params.bottomMargin;

? ? ? ? ? ? final int left = child.getRight() + params.rightMargin;

? ? ? ? ? ? final int right = left +mDivider.getIntrinsicWidth();

? ? ? ? ? ? mDivider.setBounds(left, top, right, bottom);

? ? ? ? ? ? mDivider.draw(c);

? ? ? ? }

}

private boolean isFirstColumn(RecyclerView parent, int pos, int spanCount, int childCount) {

RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

? ? ? ? if (layoutManagerinstanceof GridLayoutManager) {

if ((pos +headSize) % spanCount ==0)// 如果是第一列,則需要繪制左邊

? ? ? ? ? ? {

return true;

? ? ? ? ? ? }

}else if (layoutManagerinstanceof StaggeredGridLayoutManager) {

int orientation = ((StaggeredGridLayoutManager) layoutManager)

.getOrientation();

? ? ? ? ? ? if (orientation == StaggeredGridLayoutManager.VERTICAL) {

if ((pos +headSize) % spanCount ==0)// 如果是第一列租副,則不需要繪制右邊

? ? ? ? ? ? ? ? {

return true;

? ? ? ? ? ? ? ? }

}else {

childCount = Math.min(spanCount, childCount);

? ? ? ? ? ? ? ? if (pos <= childCount)// 如果是第一列,則不需要繪制右邊

? ? ? ? ? ? ? ? ? ? return true;

? ? ? ? ? ? }

}

return false;

? ? }

private boolean isLastColumn(RecyclerView parent, int pos, int spanCount, int childCount) {

RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

? ? ? ? if (layoutManagerinstanceof GridLayoutManager) {

if ((pos +headSize) % spanCount ==0)// 如果是最后一列亡驰,則不需要繪制右邊

? ? ? ? ? ? {

return true;

? ? ? ? ? ? }

}else if (layoutManagerinstanceof StaggeredGridLayoutManager) {

int orientation = ((StaggeredGridLayoutManager) layoutManager)

.getOrientation();

? ? ? ? ? ? if (orientation == StaggeredGridLayoutManager.VERTICAL) {

if ((pos +headSize) % spanCount ==0)// 如果是最后一列催蝗,則不需要繪制右邊

? ? ? ? ? ? ? ? {

return true;

? ? ? ? ? ? ? ? }

}else {

childCount = childCount - childCount % spanCount -headSize -1;

;

? ? ? ? ? ? ? ? if (pos >= childCount)// 如果是最后一列,則不需要繪制右邊

? ? ? ? ? ? ? ? ? ? return true;

? ? ? ? ? ? }

}

return false;

? ? }

private boolean isFirstRaw(RecyclerView parent, int pos, int spanCount,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int childCount) {

RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

? ? ? ? if (layoutManagerinstanceof GridLayoutManager) {

childCount = Math.min(spanCount, childCount);

? ? ? ? ? ? if (pos <= childCount)// 如果是第一行,則不需要繪制底部

? ? ? ? ? ? ? ? return true;

? ? ? ? }else if (layoutManagerinstanceof StaggeredGridLayoutManager) {

int orientation = ((StaggeredGridLayoutManager) layoutManager)

.getOrientation();

? ? ? ? ? ? // StaggeredGridLayoutManager 且縱向滾動(dòng)

? ? ? ? ? ? if (orientation == StaggeredGridLayoutManager.VERTICAL) {

childCount = Math.min(spanCount, childCount);

? ? ? ? ? ? ? ? // 如果是第一行票堵,則不需要繪制底部

? ? ? ? ? ? ? ? if (pos <= childCount)

return true;

? ? ? ? ? ? }else

? ? ? ? ? ? // StaggeredGridLayoutManager 且橫向滾動(dòng)

? ? ? ? ? ? {

// 如果是第一行,則不需要繪制底部

? ? ? ? ? ? ? ? if ((pos +1) % spanCount ==1) {

return true;

? ? ? ? ? ? ? ? }

}

}

return false;

? ? }

private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int childCount) {

RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

? ? ? ? if (layoutManagerinstanceof GridLayoutManager) {

childCount = childCount - childCount % spanCount -headSize -1;

? ? ? ? ? ? if (pos >= childCount)// 如果是最后一行偷溺,則不需要繪制底部

? ? ? ? ? ? ? ? return true;

? ? ? ? }else if (layoutManagerinstanceof StaggeredGridLayoutManager) {

int orientation = ((StaggeredGridLayoutManager) layoutManager)

.getOrientation();

? ? ? ? ? ? // StaggeredGridLayoutManager 且縱向滾動(dòng)

? ? ? ? ? ? if (orientation == StaggeredGridLayoutManager.VERTICAL) {

childCount = childCount - childCount % spanCount -headSize -1;

? ? ? ? ? ? ? ? // 如果是最后一行蹋辅,則不需要繪制底部

? ? ? ? ? ? ? ? if (pos >= childCount)

return true;

? ? ? ? ? ? }else

? ? ? ? ? ? // StaggeredGridLayoutManager 且橫向滾動(dòng)

? ? ? ? ? ? {

// 如果是最后一行,則不需要繪制底部

? ? ? ? ? ? ? ? if ((pos +1) % spanCount ==0) {

return true;

? ? ? ? ? ? ? ? }

}

}

return false;

? ? }

@Override

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

int spanCount = getSpanCount(parent);

? ? ? ? int position=parent.getChildAdapterPosition(view);

? ? ? ? if (position>headSize) {

int index;

? ? ? ? ? ? if(parent.getLayoutManager()instanceof StaggeredGridLayoutManager){

StaggeredGridLayoutManager.LayoutParams params =

(StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();

? ? ? ? ? ? ? ? // 當(dāng)前位置

? ? ? ? ? ? ? ? index = params.getSpanIndex();

? ? ? ? ? ? }else{

GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();

? ? ? ? ? ? ? ? index = params.getSpanIndex();

? ? ? ? ? ? }

int X = spanCount - index;

? ? ? ? ? ? if (X == spanCount) {

// 最左邊

? ? ? ? ? ? ? ? outRect.right =mWidth;

? ? ? ? ? ? ? ? outRect.left =mWidth;

? ? ? ? ? ? }else if (X ==1) {

// 最右邊

? ? ? ? ? ? ? ? outRect.right =mWidth;

? ? ? ? ? ? }else {

outRect.right =mWidth;

? ? ? ? ? ? }

}

outRect.bottom =mWidth;

? ? }

//? ? @Override

//? ? public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {

//? ? ? ? int spanCount = getSpanCount(parent);

//? ? ? ? int childCount = parent.getAdapter().getItemCount();

//? ? ? ? if (itemPosition > headSize) {

////? ? ? ? ? ? if (isLastRaw(parent, itemPosition, spanCount, childCount)) {

////? ? ? ? ? ? ? ? //如果是最后一行挫掏,則不需要繪制底部

////? ? ? ? ? ? ? ? if (isFirstColumn(parent, itemPosition, spanCount, childCount)) {

////? ? ? ? ? ? ? ? ? ? outRect.set(mWidth, 0, mWidth, mWidth * 2);

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

////? ? ? ? ? ? ? ? ? ? outRect.set(0, 0, mWidth, mWidth * 2);

////? ? ? ? ? ? ? ? }

////? ? ? ? ? ? }else

////? ? ? ? if (isLastColumn(parent, itemPosition, spanCount, childCount)){

////? ? ? ? ? ? // 如果是最后一列侦另,則不需要繪制右邊

////? ? ? ? ? ? outRect.set(0, 0, 0, mDivider.getIntrinsicHeight() + pos);

////? ? ? ? }

//? ? ? ? ? ? if (isLastRaw(parent, itemPosition, spanCount, childCount)) {

//? ? ? ? ? ? ? ? //如果是最后一行,則需要繪制底部

//? ? ? ? ? ? ? ? if (isLastColumn(parent, itemPosition, spanCount, childCount)) {

//? ? ? ? ? ? ? ? ? ? outRect.set(mWidth, mWidth, mWidth, mWidth);

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

//? ? ? ? ? ? ? ? ? ? outRect.set(mWidth, mWidth, 0, mWidth);

//? ? ? ? ? ? ? ? }

//? ? ? ? ? ? } else if (isLastColumn(parent, itemPosition, spanCount, childCount)) {

//? ? ? ? ? ? ? ? // 如果是最后一列尉共,則需要繪制右邊

//? ? ? ? ? ? ? ? outRect.set(mWidth, mWidth, mWidth, 0);

//? ? ? ? ? ? } else {

//? ? ? ? ? ? ? ? outRect.set(mWidth, mWidth, 0, 0);

//? ? ? ? ? ? }

//? ? ? ? }

//

//

//? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末褒傅,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子袄友,更是在濱河造成了極大的恐慌殿托,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件剧蚣,死亡現(xiàn)場(chǎng)離奇詭異支竹,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)鸠按,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)礼搁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人目尖,你說(shuō)我怎么就攤上這事馒吴。” “怎么了瑟曲?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵募书,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我测蹲,道長(zhǎng)莹捡,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任扣甲,我火速辦了婚禮篮赢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘琉挖。我一直安慰自己启泣,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布示辈。 她就那樣靜靜地躺著寥茫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪矾麻。 梳的紋絲不亂的頭發(fā)上纱耻,一...
    開(kāi)封第一講書(shū)人閱讀 51,692評(píng)論 1 305
  • 那天芭梯,我揣著相機(jī)與錄音,去河邊找鬼弄喘。 笑死玖喘,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蘑志。 我是一名探鬼主播累奈,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼急但!你這毒婦竟也來(lái)了澎媒?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤波桩,失蹤者是張志新(化名)和其女友劉穎旱幼,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體突委,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年冬三,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了匀油。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡勾笆,死狀恐怖敌蚜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情窝爪,我是刑警寧澤弛车,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站蒲每,受9級(jí)特大地震影響纷跛,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜邀杏,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一贫奠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧望蜡,春花似錦唤崭、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至小泉,卻和暖如春芦疏,著一層夾襖步出監(jiān)牢的瞬間冕杠,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工眯分, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拌汇,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓弊决,卻偏偏與公主長(zhǎng)得像噪舀,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子飘诗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355

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

  • RecyclerView在項(xiàng)目開(kāi)發(fā)中使用的頻率還是相當(dāng)高的与倡,平時(shí)在網(wǎng)上看到文章大體都有介紹,總感覺(jué)有些欠缺昆稿,最近空...
    lainuo閱讀 1,164評(píng)論 0 3
  • 目錄介紹 01.規(guī)則瀑布流實(shí)現(xiàn) 02.不規(guī)則瀑布流實(shí)現(xiàn)2.1 實(shí)現(xiàn)方式2.2 遇到問(wèn)題 03.瀑布流上拉加載 04...
    楊充211閱讀 6,130評(píng)論 1 12
  • LinearLayoutManager情況 public class DividerItemDecoration ...
    43d60efa37c7閱讀 721評(píng)論 0 0
  • 久違的晴天纺座,家長(zhǎng)會(huì)。 家長(zhǎng)大會(huì)開(kāi)好到教室時(shí)溉潭,離放學(xué)已經(jīng)沒(méi)多少時(shí)間了净响。班主任說(shuō)已經(jīng)安排了三個(gè)家長(zhǎng)分享經(jīng)驗(yàn)。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,523評(píng)論 16 22
  • 今天感恩節(jié)哎喳瓣,感謝一直在我身邊的親朋好友馋贤。感恩相遇!感恩不離不棄畏陕。 中午開(kāi)了第一次的黨會(huì)配乓,身份的轉(zhuǎn)變要...
    迷月閃星情閱讀 10,567評(píng)論 0 11