Android:Gridview高度测量不正常,Gridview的高度大于item铺满的高度,底部部有留白

问题出现背景: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>

遇到问题,按步分析,总会找到问题所在

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值