实现均分屏幕的GridLayout-CellLayout



package com.example.testgridlayout;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;

/**
 * 
 * @className: CellLayout
 * @description: 给ViewPager每一页使用的GridLayout, 均分屏幕 默认是3行3列 可以设置行距,
 *               不能在ScrollView中使用
 * @author: gaoshuai
 * @date: 2015年9月25日 上午10:46:55
 */
public class CellLayout extends ViewGroup
{
    private int lineNum = 3;
    private int columnNum = 3;
    private int vecitcalSpace;
    private int horizontalSpace;
    private int mCellWidth;
    private int mCellHeight;
    
    public CellLayout(Context context) {
        super(context);
    }
    
    public CellLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CellLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    protected void dispatchDraw(Canvas canvas) {
        final long drawingTime = getDrawingTime();
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            drawChild(canvas, getChildAt(i), drawingTime);
        }
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException("CellLayout can only be used in EXACTLY mode.");
        }
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException("CellLayout can only be used in EXACTLY mode.");
        }
        //        int layoutWidth = this.getWidth();
        //        int layoutHeight = this.getHeight();
        int layoutHeight = MeasureSpec.getSize(heightMeasureSpec);
        int layoutWidth = MeasureSpec.getSize(widthMeasureSpec);
        mCellWidth = 0;
        mCellHeight = 0;
        if (this.columnNum > 0) {
            //cellWidth = layoutWidth/this.columnNum - (this.columnNum + 1) * this.horizontalSpace;
            mCellWidth = (layoutWidth - (this.columnNum + 1) * this.horizontalSpace) / this.columnNum;
        }
        if (this.lineNum > 0) {
            mCellHeight = (layoutHeight - (this.lineNum + 1) * this.vecitcalSpace) / this.lineNum;
            
        }
        
        // The children are given the same width and height as the workspace
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            //getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
            getChildAt(i).measure(MeasureSpec.makeMeasureSpec(mCellWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mCellHeight, MeasureSpec.EXACTLY));
        }
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int count = getChildCount();
        int index = 0;
        for (int i = 0; i < this.lineNum; i++) {
            for (int j = 0; j < this.columnNum; j++) {
                if (index >= count) {
                    break;
                }
                
                int x = j * mCellWidth + (j + 1) * this.horizontalSpace;
                int y = i * mCellHeight + (i + 1) * this.vecitcalSpace;
                int width = mCellWidth;
                int height = mCellHeight;
                
                final View child = getChildAt(index);
                
                if (child.getVisibility() != View.GONE) {
                    child.layout(x, y, x + width, y + height);
                }
                
                index++;
            }
        }
    }
    
    public void setHorizontalSpace(int space) {
        this.horizontalSpace = space;
    }
    
    public void setVecticalSpace(int space) {
        this.vecitcalSpace = space;
    }
    
    public int getLineNum() {
        return lineNum;
    }
    
    public void setLineNum(int lineNum) {
        this.lineNum = lineNum;
    }
    
    public int getColumnNum() {
        return columnNum;
    }
    
    public void setColumnNum(int columnNum) {
        this.columnNum = columnNum;
    }
    
}



转载于:https://my.oschina.net/sfshine/blog/511021

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值