2
其中一個Android示例(FixedGridLayout
)將ViewGroup
擴展爲允許在將新項添加到網格時進行自定義轉換。代碼按預期工作,但不實現滾動。因此,我將整個佈局包裝在一個ScrollView
期望這將解決問題。但是,看起來FixedGridLayout
視圖實際上比它應該大得多,在項目之後留下了很多可滾動的空間。擴展ViewGroup時正確實施onMeasure()
我懷疑這個問題與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));
}