RecycleView之GridLayoutItemDecoration等距分割線

1.自定義分割線


public class GridLayoutItemDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDriver;

    // 網(wǎng)上絕大部分用的系統(tǒng)的一個(gè)屬性 叫做 android.R.attrs.listDriver 褒侧,這個(gè)也可以恨课,需要在清單文件中配置
    public GridLayoutItemDecoration(Context context, int drawableRescourseId) {
        // 解析獲取 Drawable
        mDriver = ContextCompat.getDrawable(context, drawableRescourseId);
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        // 留出分割線的位置  每個(gè)item控件的下邊和右邊
        outRect.bottom = mDriver.getIntrinsicHeight();


        GridLayoutManager mgr;
        if (parent.getLayoutManager() instanceof GridLayoutManager) {
            mgr = (GridLayoutManager) parent.getLayoutManager();
        } else {
            return;
        }
        int position = parent.getChildAdapterPosition(view);

        if (position == RecyclerView.NO_POSITION) {
            // here I need to access the position of the current element
            // and call outRect.set(left, top , right, bottom);
            // which is not possible because it is no longer in the adapter
            return;
        }

        int spanCount = mgr.getSpanCount();
        int spanSize = mgr.getSpanSizeLookup().getSpanSize(position);
        int spanIndex = mgr.getSpanSizeLookup().getSpanIndex(position, spanCount);

        if (spanIndex == spanCount - 1) {
            // last element

            outRect.left = mDriver.getIntrinsicWidth() / 2;
            outRect.right = mDriver.getIntrinsicWidth();

        } else if (spanIndex == 0) {
            // first element
            outRect.left = mDriver.getIntrinsicWidth();
            outRect.right = mDriver.getIntrinsicWidth() / 2;

        } else {
            // middle element
            outRect.left = mDriver.getIntrinsicWidth()/2;
            outRect.right = mDriver.getIntrinsicWidth() / 2;
        }
    }

    // 繪制分割線
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        // 繪制分割線

        // 繪制水平方向
        drawHorizontal(c, parent);
//         繪制垂直方向
        drawVertical(c, parent);
    }


    /**
     * 繪制垂直方向的分割線
     *
     * @param c
     * @param parent
     */
    private void drawVertical(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) childView.getLayoutParams();

            int top = childView.getTop() - params.topMargin;
            int bottom = childView.getBottom() + params.bottomMargin;
            int left = childView.getRight() + params.rightMargin;
            int right = left + mDriver.getIntrinsicWidth();

            //計(jì)算水平分割線的位置
            mDriver.setBounds(left, top, right, bottom);
            mDriver.draw(c);
        }
    }


    /**
     * 繪制水平方向的分割線
     *
     * @param c
     * @param parent
     */
    private void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) childView.getLayoutParams();

            int left = childView.getLeft() - params.leftMargin;
            int right = childView.getRight() + mDriver.getIntrinsicWidth() + params.rightMargin;
            int top = childView.getBottom() + params.bottomMargin;
            int bottom = top + mDriver.getIntrinsicHeight();

            //計(jì)算水平分割線的位置
            mDriver.setBounds(left, top, right, bottom);
            mDriver.draw(c);
        }
    }
}

創(chuàng)建分割線顏色和大小drawable\item_divider_white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">


   
    <solid android:color="@color/white" />
    <size
        android:width="15dp"
        android:height="15dp" />
</shape>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末奄妨,一起剝皮案震驚了整個(gè)濱河市镀虐,隨后出現(xiàn)的幾起案子纫版,更是在濱河造成了極大的恐慌,老刑警劉巖拾弃,帶你破解...
    沈念sama閱讀 219,366評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件串纺,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡挺物,警方通過(guò)查閱死者的電腦和手機(jī)懒浮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)识藤,“玉大人砚著,你說(shuō)我怎么就攤上這事〕彰粒” “怎么了稽穆?”我有些...
    開封第一講書人閱讀 165,689評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)赶撰。 經(jīng)常有香客問(wèn)我舌镶,道長(zhǎng)柱彻,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,925評(píng)論 1 295
  • 正文 為了忘掉前任餐胀,我火速辦了婚禮哟楷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘否灾。我一直安慰自己卖擅,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評(píng)論 6 392
  • 文/花漫 我一把揭開白布墨技。 她就那樣靜靜地躺著惩阶,像睡著了一般。 火紅的嫁衣襯著肌膚如雪扣汪。 梳的紋絲不亂的頭發(fā)上琳猫,一...
    開封第一講書人閱讀 51,727評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音私痹,去河邊找鬼脐嫂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛紊遵,可吹牛的內(nèi)容都是我干的账千。 我是一名探鬼主播,決...
    沈念sama閱讀 40,447評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼暗膜,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼匀奏!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起学搜,我...
    開封第一講書人閱讀 39,349評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤娃善,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后瑞佩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體聚磺,經(jīng)...
    沈念sama閱讀 45,820評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評(píng)論 3 337
  • 正文 我和宋清朗相戀三年炬丸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了瘫寝。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,127評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡稠炬,死狀恐怖焕阿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情首启,我是刑警寧澤暮屡,帶...
    沈念sama閱讀 35,812評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站毅桃,受9級(jí)特大地震影響褒纲,放射性物質(zhì)發(fā)生泄漏准夷。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評(píng)論 3 331
  • 文/蒙蒙 一外厂、第九天 我趴在偏房一處隱蔽的房頂上張望冕象。 院中可真熱鬧,春花似錦汁蝶、人聲如沸渐扮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)墓律。三九已至,卻和暖如春幔亥,著一層夾襖步出監(jiān)牢的瞬間耻讽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工帕棉, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留针肥,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,388評(píng)論 3 373
  • 正文 我出身青樓香伴,卻偏偏與公主長(zhǎng)得像慰枕,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子即纲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評(píng)論 2 355

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