how to make dynamic diagonal view list

后端 未结 1 775
轻奢々
轻奢々 2021-02-06 12:14

i m adding diagonal cut layout to RecyclerView, but i m not getting expected result. my second view start with end of first view, and thats obvious. bu

相关标签:
1条回答
  • 2021-02-06 12:41

    Change your CutLayout to have the same path on top like so:

    public class CutLayout extends FrameLayout {
        private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private Xfermode pdMode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
        private Path path = new Path();
    
        public CutLayout(Context context) {
            super(context);
        }
    
        public CutLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CutLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public CutLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        @Override
        protected void dispatchDraw(Canvas canvas) {
            int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
            super.dispatchDraw(canvas);
    
            paint.setXfermode(pdMode);
            path.reset();
    
            path.moveTo(0, 0);
            path.lineTo(getWidth(), 0);
            path.lineTo(0, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
            path.close();
            canvas.drawPath(path, paint);
    
            path.reset();
    
            path.moveTo(0, getHeight());
            path.lineTo(getWidth(), getHeight());
            path.lineTo(getWidth(), getHeight() - TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
            path.close();
            canvas.drawPath(path, paint);
    
            canvas.restoreToCount(saveCount);
            paint.setXfermode(null);
        }
    }
    

    Then all you have to do is to create an item decorator for your Recycler view that can accept a negative top margin

    public class NegativeTopItemDecorator extends RecyclerView.ItemDecoration{
        private final int mTopMargin;
    
        public NegativeTopItemDecorator(int topMargin) {
            this.mTopMargin = topMargin;
        }
    
    
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.top = mTopMargin;
        }
    }
    

    And add it to your RecyclerView

    int topMargin = - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
    NegativeTopItemDecorator itemDecorator = new NegativeTopItemDecorator(topMargin);
    mRecyclerView.addItemDecoration(itemDecorator);
    

    This will give you something like the following output

    0 讨论(0)
提交回复
热议问题