CellLayout是workspace的屏幕。
CellLayout中的一些重要属性:
int mCellWidth;
int mCellHeight;
//每一个屏幕的行列数
int mCountX;//每一行的item个数
int mCountY;//每一列的item个数
//item之间的距离
int mWidthGap;//item之间的宽度
int mHeightGap;//item之间的高度
构造方法
public CellLayout(Context context, AttributeSet attrs, int defStyle){
super(context,attrs,defStyle){
setWillNotDraw(false);
setClipToPadding(false);
mLauncher = (Launcher)context;
DeviceProfile grid = mLauncher.getDeviceProfile();
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CellLayout,defStyle,0);
mCellWidth = mCellHeight = -1;
mFixedCellWidth = mFixedCellHeight = -1;
//item之间的距离
mWidthGap = mOriginalWidthGap = 0;
mHeightGap = mOriginalHeightGap = 0;
mMaxGap = Integer.MAX_VALUE;
//每屏幕上的item行数列数
//DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();grid是DeviceProfile的对象
mCountX = (int) grid.inv.numColumns;
mcountY = (int) grid.inv.numRows;
mOccupied = new boolean[mCountX][mCountY];
mTmpOccupied = new boolean[mCountX][mCountY];
……
final Resources res = getResources();
mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
//CellLayout缩略图背景,即长按屏幕空白处时CellLayout缩小时的背景图
mBackground = (TransitionDrawable) res.getDrawable(R.drawable.bg_screenpanel);
mBackground.setCallback(this);
……
}
}
在Workspace.java中加载CellLayout的布局,代码如下:
public long insertNewWorkspaceScreen(long screenId, int insertIndex){
if (mWorkspaceScreens.containsKey(screenId)) {
throw new RuntimeException("Screen id " + screenId + " already exists!");
}
// Inflate the cell layout, but do not add it automatically so //that we can get the newly created CellLayout.
//加载CellLayout布局
CellLayout newScreen = (CellLayout)mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen,this,false);
}
CellLayout中的一个item可以占多个Cell,CellInfo记录item的起始坐标、占多少格
public static final class CellInfo{
View cell;
int cellX = -1;
int cellY = -1;
int spanX;
int spanY;
long screenId;
long container;
public CellInfo(View v, ItemInfo info){
cell = v;
cellX = info.cellX;
cellY = info.cellY;
spanX = info.spanX;
spanY = info.spanY;
screenId = info.screenId;
container = info.cotainer;
}
}
CellLayout的触摸事件
public boolean onInterceptTouchEvent(MotionEvent ev){
if(mUseTouchHelper || (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this,ev)){
return true;
}
return false;
}
public boolean onTouchEvent(MotionEvent ev){
boolean handled = super.onTouchEvent(ev);
//检查workspace是否处在预览模式
if(mLauncher.mWorkspace.isInOverviewMode() && mStylusEventHelper.checkAndPerformStylusEvent(ev)){
return true;
}
return handled;
}
设置cell间距、占格数的大小
public void setCellDimensions(int width, int height);
public void setGridSize(int x, int y);