美文网首页
自定义ItemDecoration_实现RecyclerView

自定义ItemDecoration_实现RecyclerView

作者: BraveJoy | 来源:发表于2019-04-02 15:05 被阅读0次

    一般我们在做recyclerview分隔线的时候都是只实现内部而不考虑四周的外部,那么如何实现这样的效果呢,其实只是在原来的基础加一些判断在四周再多绘制一些线条即可。效果图如下:

    自定义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();
    
                /**
                 * 画水平分隔线
                 */
                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);
    
    
                /**
                 * 画垂直分割线
                 */
                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;
            }
        }
    
        /**
         * 列数
         *
         * @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);
        }
    }
    

    相关文章

      网友评论

          本文标题:自定义ItemDecoration_实现RecyclerView

          本文链接:https://www.haomeiwen.com/subject/lvlgbqtx.html