Android 示例之一 ( FixedGridLayout
) 扩展了 aViewGroup
以允许在将新项目添加到网格时进行自定义转换。该代码按预期工作,但没有实现滚动。因此,我将整个布局包装起来,ScrollView
期望这可以解决问题。然而,看起来FixedGridLayout
视图实际上比它应该的大得多,在项目之后留下了很多可滚动的空间。
我怀疑这个问题与 onMeasure() 的实施方式有关。我是对的,如果是这样,这段代码有什么问题?
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int cellWidthSpec = MeasureSpec.makeMeasureSpec(mCellWidth, MeasureSpec.AT_MOST);
int cellHeightSpec = MeasureSpec.makeMeasureSpec(mCellHeight, MeasureSpec.AT_MOST);
int count = getChildCount();
for (int index=0; index<count; index++) {
final View child = getChildAt(index);
child.measure(cellWidthSpec, cellHeightSpec);
}
// Use the size our parents gave us, but default to a minimum size to avoid
// clipping transitioning children
int minCount = count > 3 ? count : 3;
setMeasuredDimension(resolveSize(mCellWidth * minCount, widthMeasureSpec),
resolveSize(mCellHeight * minCount, heightMeasureSpec));
}