我expandable height GridView
在我的片段中使用。onMeasure
它是带有重写方法的默认 GridView 。这允许GridView
在ScrollView
其中使用此 GridView 包装片段。
boolean expanded = false;
public boolean isExpanded() {
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
但是我在检查GridView
屏幕上显示的项目时遇到了问题。所有可以帮助我的方法都不适用于覆盖,因为所有项目在创建onMeasure
时一次显示。GridView
getScrollY() - 总是返回 0,因为 GridView 已经被覆盖的 onMeasure 扩展并且不会滚动。
onScrollListener's
参数只是从 0 到最后一个项目的一系列可见子视图,因为它们都显示(通过 onMeasure),而不是实际的滚动位置。