Gridview 在布局时会多次调用onMeasure 来测量宽高 造成item0的getView()被多次调用,调试后可以发现GridView的onMeasure 会被多次调用而onLayout 则是正确调用的。 只需要加一个标志位,在getview中进行判断即可
- YourGridView.java
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
isOnMeasure = false;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
isOnMeasure = true;
super.onLayout(changed, l, t, r, b);
}
- YourGridViewAdapter.java
public View getView(int position, View convertView, ViewGroup parent) {
if(((YourGridView) parent).isMeasure()){
//do something
}
}