android gridview row dividers / separators

后端 未结 4 1678
臣服心动
臣服心动 2020-11-27 13:53

Is there a way to show (horizontal) dividers between rows in a gridview?

I tried putting a small divider-image below every grid item, but this is not a solution, bec

4条回答
  •  一整个雨季
    2020-11-27 14:28

    Just wanted to share how I implemented this using the link accepted by OP. For my case I also needed to control the length of the separators, so I couldn't get around subclassing GridView.

    public class HorizontalSeparatorGridView extends GridView {
    
        // Additional methods 
    
        @Override
        protected void dispatchDraw(Canvas canvas) {
    
            final int count = getChildCount();
            for(int i = 0; i < count; i++) {
                View child = getChildAt(i);
                int bottom = child.getBottom();
                int left = child.getLeft();
                int right = child.getRight();
    
                Paint paint = new Paint();
                paint.setColor(0xffececec);
    
                paint.setStrokeWidth(Math.round(0.5 * density));
    
                int offset = // Some offset
    
                canvas.drawLine(left + offset, bottom, right - offset, bottom, paint);
            }
    
    
            super.dispatchDraw(canvas);
        }
    

    I subclassed dispatchDraw as opposed to onDraw just to be safe but I don't think it would matter in this case.

提交回复
热议问题