问题出现背景:scrollview嵌套gridview,gridview只显示一张网络请求的图片
首先我自定义Gridview重写了onMeasure方法(为了解决嵌套,gridview显示一行的问题)
@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); Log.d("hieght",expandSpec+""); }
问题如下图:
第一张为正常显示,第二张为异常显示
我的item布局是这样的:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:scaleType="fitXY" /> </FrameLayout>
出现这个问题主要是网络图片的原因,当网络图片显示到imageview时会触发gridview的onMeasure方法,由于九张图片是异步加载,多次触发gridview的onMeasure方法还会出现闪动的问题,由此gridview的onMeasure方法测量时会出现误差,神奇的是退出这个页面再进入,有会显示正常,一开始感觉很神奇摸不着头脑,其实也没这么玄幻,是因为网络图片加载框架一般都会对加载过的图片进行缓存,也就是说在缓存图片还在的情况下,图片的高度已经已知,gridview在测量时就不会出现误差,也不会闪动了(相当于显示了一张本地图片),找到了问题所在,解决办法也就出来了,没错就是把imageview的高度写死
即:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_img"
android:layout_width="match_parent"
android:layout_height="110dp"
android:layout_gravity="center"
android:scaleType="fitXY" />
</FrameLayout>
遇到问题,按步分析,总会找到问题所在