模仿支付宝GridView 分割线实现

先上效果图:


上代码:

package com.integration.utils.ui;

import com.integration.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;

public class MyGridView extends GridView {
	public MyGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyGridView(Context context) {
		super(context);
	}

	public MyGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		View localView1 = getChildAt(0);
		int column = getWidth() / localView1.getWidth();
		int childCount = getChildCount();
		Paint localPaint = new Paint();
		localPaint.setStyle(Paint.Style.STROKE);
		localPaint.setColor(getContext().getResources().getColor(R.color.line));
		for (int i = 0; i < childCount; i++) {
			View cellView = getChildAt(i);
			if ((i + 1) % column == 0) {
				// 绘制顶部
				canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getRight(), cellView.getTop(),
						localPaint);
				// 最右侧只画底部边框
				canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(),
						localPaint);
			} else if ((i + 1) > (childCount - (childCount % column))) {
				// 绘制顶部(此处多行时候没有判断,会重复绘制)
				canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getRight(), cellView.getTop(),
						localPaint);
				// 最下部只画右侧边框
				canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(),
						localPaint);
				// 绘制底部
				canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(),
						localPaint);
			} else {
				// 绘制顶部
				canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getRight(), cellView.getTop(),
						localPaint);
				// 画底部和右侧边框
				canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(),
						localPaint);
				canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(),
						localPaint);
			}
		} // 如果最后一行不足,则补全缺省item的右侧竖线。
		if (childCount % column != 0) {
			for (int j = 0; j < (column - childCount % column); j++) {
				View lastView = getChildAt(childCount - 1);
				// 绘制顶部(此处多行时候没有判断,会重复绘制)
				canvas.drawLine(lastView.getRight() + lastView.getWidth() * j, lastView.getTop(),
						lastView.getRight() + lastView.getWidth() * (j + 1), lastView.getTop(), localPaint);
				//右侧竖线
				canvas.drawLine(lastView.getRight() + lastView.getWidth() * j, lastView.getTop(),
						lastView.getRight() + lastView.getWidth() * j, lastView.getBottom(), localPaint);
				// 绘制底部
				canvas.drawLine(lastView.getRight() + lastView.getWidth() * j, lastView.getBottom(),
						lastView.getRight() + lastView.getWidth() * (j + 1), lastView.getBottom(), localPaint);
			}
		}
	}

}

<com.integration.utils.ui.MyGridView
            android:id="@+id/gridview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#fff"
            android:horizontalSpacing="0.0dip"
            android:listSelector="@null"
            android:numColumns="4"
            android:scrollbars="none"
            android:stretchMode="columnWidth"
            android:verticalSpacing="0.0dip" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值