RecyclerView 宮格分割線ItemDecoration實現(xiàn)

效果如下

image.png

分解:
1闻书、自定RecyclerView.ItemDecoration
2般哼、通過getItemOffsets 給每一個 Item 設置偏移量,相當于留出間距
3惠窄、通過onDraw 在留出的間距上繪制想要的邊線

源碼:

//主要是繪制了每一個item 的右邊和下邊線條蒸眠,最后一列的右邊沒有繪制,最后一行的底部不做繪制
public class GridSpaceItemDecoration extends RecyclerView.ItemDecoration {
    private int space;
    private Paint paint = new Paint();

    public GridSpaceItemDecoration() {
        space = SizeUtils.dp2px(1f);
        paint.setColor(Color.parseColor("#f7f7f7"));
        paint.setStrokeWidth(space);
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        drawHorizontal(c, parent);
        drawVertical(c, parent);
    }

    //圖中橫向的紅色線條繪制杆融,為了便于區(qū)分設置的兩個顏色楞卡,代碼里已經(jīng)去掉
    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 + space;
            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + space;
            c.drawRect(new Rect(left, top, right, bottom), paint);
        }
    }

   //圖中縱向的綠色線條繪制,為了便于區(qū)分設置的兩個顏色脾歇,代碼里已經(jīng)去掉
    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 + space;
            c.drawRect(new Rect(left, top, right, bottom), paint);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                               RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int itemPosition = parent.getChildLayoutPosition(view);
        int spanCount = getSpanCount(parent);
        double count = Math.ceil((double) parent.getAdapter().getItemCount() / (double) spanCount);//總行數(shù)
        //double currentCount = Math.ceil((double) (itemPosition + 1) / spanCount);//當前行數(shù)
        if (count == 1) {
            // 如果是最后一列蒋腮,則不需要繪制右邊
            if (isLastColumn(itemPosition, parent)) {
                outRect.set(0, 0, 0, 0);
            } else {
                outRect.set(0, 0, space, 0);
            }
        } else {
            if (isLastRow(itemPosition, parent)) {
                if (isLastColumn(itemPosition, parent)) {
                    outRect.set(0, 0, 0, 0);
                } else {
                    outRect.set(0, 0, space, 0);
                }
            } else {
                if (isLastColumn(itemPosition, parent)) {
                    outRect.set(0, 0, 0, space);
                } else {
                    outRect.set(0, 0, space, space);
                }
            }
        }
    }


    private boolean isLastRow(int itemPosition, RecyclerView parent) {
        int spanCount = getSpanCount(parent);
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        //有多少列
        if (layoutManager instanceof GridLayoutManager) {
            int childCount = parent.getAdapter().getItemCount();

            double count = Math.ceil((double) childCount / (double) spanCount);//總行數(shù)
            double currentCount = Math.ceil((double) (itemPosition + 1) / spanCount);//當前行數(shù)

            //最后當前數(shù)量小于總的
            if (currentCount < count) {
                return false;
            }
        }
        return true;
    }

    private boolean isLastColumn(int itemPosition, RecyclerView parent) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        //有多少列
        if (layoutManager instanceof GridLayoutManager) {
            int spanCount = getSpanCount(parent);
            if ((itemPosition + 1) % spanCount == 0) {//因為是從0可以所以要將ItemPosition先加1
                return true;
            }
        }
        return false;
    }

    private int getSpanCount(RecyclerView parent) {
        if (parent.getLayoutManager() instanceof GridLayoutManager) {
            return ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
        } else {
            return 1;
        }
    }
}



歡迎大家留言討論

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市藕各,隨后出現(xiàn)的幾起案子池摧,更是在濱河造成了極大的恐慌,老刑警劉巖激况,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件作彤,死亡現(xiàn)場離奇詭異,居然都是意外死亡乌逐,警方通過查閱死者的電腦和手機竭讳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浙踢,“玉大人绢慢,你說我怎么就攤上這事÷宀ǎ” “怎么了胰舆?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蹬挤。 經(jīng)常有香客問我缚窿,道長,這世上最難降的妖魔是什么闻伶? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任滨攻,我火速辦了婚禮够话,結果婚禮上蓝翰,老公的妹妹穿的比我還像新娘光绕。我一直安慰自己,他們只是感情好畜份,可當我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布诞帐。 她就那樣靜靜地躺著,像睡著了一般爆雹。 火紅的嫁衣襯著肌膚如雪停蕉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天钙态,我揣著相機與錄音慧起,去河邊找鬼。 笑死册倒,一個胖子當著我的面吹牛蚓挤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播驻子,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼灿意,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了崇呵?” 一聲冷哼從身側響起缤剧,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎域慷,沒想到半個月后荒辕,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡犹褒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年兄纺,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片化漆。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡估脆,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出座云,到底是詐尸還是另有隱情疙赠,我是刑警寧澤,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布朦拖,位于F島的核電站圃阳,受9級特大地震影響,放射性物質發(fā)生泄漏璧帝。R本人自食惡果不足惜捍岳,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧锣夹,春花似錦页徐、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至贴唇,卻和暖如春搀绣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背戳气。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工链患, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瓶您。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓锣险,卻偏偏與公主長得像,于是被迫代替她去往敵國和親览闰。 傳聞我的和親對象是個殘疾皇子芯肤,可洞房花燭夜當晚...
    茶點故事閱讀 44,955評論 2 355

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