自定義ItemDecoration_實(shí)現(xiàn)RecyclerView的item四周都有分隔線

一般我們?cè)谧鰎ecyclerview分隔線的時(shí)候都是只實(shí)現(xiàn)內(nèi)部而不考慮四周的外部,那么如何實(shí)現(xiàn)這樣的效果呢,其實(shí)只是在原來(lái)的基礎(chǔ)加一些判斷在四周再多繪制一些線條即可。效果圖如下:

自定義ItemDecoration

自定義ItemDecoration繼承RecyclerView.ItemDecoration

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * 自定義GridLayoutManager分割線,只適用于GridLayoutManager
 */
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {
    private Paint mPaint;
    private int mDividerWidth;

    public GridDividerItemDecoration(int height, @ColorInt int color) {
        mDividerWidth = height;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);


        int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        int spanCount = getSpanCount(parent);

        boolean isfirsColumn = isfirsColumn(itemPosition, spanCount);
        boolean isLastColumn = isLastColumn(itemPosition, spanCount);

        boolean isfirstRow = isfirstRow(itemPosition, spanCount);

        int top;
        int left;
        int right;
        int bottom;

        int eachWidth = (spanCount - 1) * mDividerWidth / spanCount;

        int dl = mDividerWidth - eachWidth;

        left = itemPosition % spanCount * dl;

        right = eachWidth - left;


        bottom = mDividerWidth;

        if (isfirstRow) {
            top = mDividerWidth;
        } else {
            top = 0;
        }

        if (isfirsColumn) {
            left = mDividerWidth;
        }
        if (isLastColumn) {
            right = mDividerWidth;
        }


        outRect.set(left, top, right, bottom);
    }

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

    private void draw(Canvas canvas, RecyclerView parent) {
        int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();

            /**
             * 畫(huà)水平分隔線
             */
            int left = child.getLeft();
            int right = child.getRight();
            int top = child.getBottom() + layoutParams.bottomMargin;
            int bottom = top + mDividerWidth;
            canvas.drawRect(left, top, right, bottom, mPaint);


            /**
             * 畫(huà)垂直分割線
             */
            top = child.getTop();
            bottom = child.getBottom() + mDividerWidth;
            left = child.getRight() + layoutParams.rightMargin;
            right = left + mDividerWidth;
            canvas.drawRect(left, top, right, bottom, mPaint);

            int spanCount = getSpanCount(parent);

            /**
             * 如果是第一行
             */
            if (isfirstRow(i, spanCount)) {
                canvas.drawRect(0, 0, right, mDividerWidth, mPaint);
            }

            /**
             * 如果是第一列
             */
            if (isfirsColumn(i, spanCount)) {
                canvas.drawRect(0, 0, mDividerWidth, bottom, mPaint);
            }

        }
    }

    /**
     * 判斷是不是第一行
     *
     * @param pos
     * @param spanCount
     * @return
     */
    private boolean isfirstRow(int pos, int spanCount) {
        if ((pos / spanCount + 1) == 1) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 判斷是不是第一列
     *
     * @param pos
     * @param spanCount
     * @return
     */
    private boolean isfirsColumn(int pos, int spanCount) {
        if (pos % spanCount == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判斷是不是最后一列
     *
     * @param pos
     * @param spanCount
     * @return
     */
    private boolean isLastColumn(int pos, int spanCount) {
        if ((pos - spanCount + 1) % spanCount == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 列數(shù)
     *
     * @param parent
     * @return
     */
    private int getSpanCount(RecyclerView parent) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        return ((GridLayoutManager) layoutManager).getSpanCount();
    }


}

如何使用?

private void initView() {
    recyclerview.setLayoutManager(new GridLayoutManager(mContext, 4));
    recyclerview.addItemDecoration(new GridDividerItemDecoration(ScreenUtils.dip2px(mContext, 1)
            , ContextCompat.getColor(mContext, R.color.colorAccent)));
    recyclerview.setAdapter(new RecyclerView.Adapter() {
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            TextView textView = new TextView(mContext);
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                    , ScreenUtils.dip2px(mContext, 80)));
            textView.setGravity(Gravity.CENTER);
            textView.setBackgroundColor(Color.WHITE);
            return new MyViewHolder(textView);
        }
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            ((TextView) (viewHolder.itemView)).setText("" + i);
        }
        @Override
        public int getItemCount() {
            return 14;
        }
    });
}
private class MyViewHolder extends RecyclerView.ViewHolder {
    public MyViewHolder(View itemView) {
        super(itemView);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容