直奔主題:
GridView放在ScrollerView中是無法正常顯示的,這個問題網上討論比較多,大家也都比較清楚,主要做法是自定義一個GridView,如下:
package com.ecity.xinao.ui.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;
public class PhotoHistoryItemGridView extends GridView {
public PhotoHistoryItemGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public PhotoHistoryItemGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PhotoHistoryItemGridView(Context context) {
super(context);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
但我這樣做了之後,發現一個問題,當這個GridView放入ScrollView中時,當高度不超過手機屏幕時,是可以正常顯示的,但是當高度超過手機時,發現GridView中的Item都不能正常顯示了,一片空白,如下圖
百度好久,未果。。。。
發現我的代碼中有一行:sectionViewPhoto.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
大家應該都知道,這是開啓硬件加速的,這是View用來關閉硬件加速的,爲了讓view顯示更平滑,在這裏我是爲了顯示用shape畫的虛線。
但是 目前,Android對硬件加速的支持並非完美,有些繪製操作在開啓硬件加速的情況下不能正常工作(具體的列表可以參考Android開發者文檔)。
註釋掉這行代碼後,GridView顯示正常:
希望能幫到一些朋友。