美文网首页
ItemDecoration随笔小计

ItemDecoration随笔小计

作者: 二十三岁的梦 | 来源:发表于2018-09-30 11:28 被阅读0次
    public class SimpleItemDecoration extends RecyclerView.ItemDecoration {
        private Paint paint;
        private Context context;
    
        public SimpleItemDecoration(Context context) {
            this.context=context;
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(ContextCompat.getColor(context,R.color.colorDivider));
        }
    
        private int itemHeight;
    
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            itemHeight = (int) (1 * context.getResources().getDisplayMetrics().density);
            outRect.bottom = itemHeight;
        }
    
        @Override
        public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.onDraw(c, parent, state);
            float paddingLeftOrRight = 12.0f * context.getResources().getDisplayMetrics().density;
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount - 1; i++) {
                View view = parent.getChildAt(i);
                c.drawRect(view.getLeft() + paddingLeftOrRight,
                        view.getBottom(),
                        view.getRight() - paddingLeftOrRight,
                        view.getBottom() + itemHeight,
                        paint);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:ItemDecoration随笔小计

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