ScrollView包裹listView、ScrollView包裹tablayout+ViewPager不显示问题的解决

1.ScrollView包裹ListView,listView会显示1-2行或者不显示,

此时需要重新listView重写onMeasure()方法;

public class MyListView extends ListView{

    // 构造方法

    。。。。。。

    // 重写测量方法

    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){

    int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);

    super.onMeasure(widthMeasureSpec,heightSpec);

    }

}

但是这样重写后就会一次性将listView里面的item全都显示了,比较耗内存,推荐使用RecyclerView;


2.ScrollView包裹tablayout+viewPager;

tablayout会显示,但viewPager绑定的Fragment会不显示;

这时候直接自xml文件里ScrollView添加:

android:fillViewport="true"

就可以正常显示了,也可以重新自定义一个ViewPager类,重新测量:

 // 重写测量方法

    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){

    int height=0;

    int heightSpec;

    for(int i=0;i<getChildCount();i++){

    View child = getChildAt(i);

    child.measure(widthMeasureSpec,MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));

    int h=child.getMeasuredHeight();

      if(height<h){

        height = h;

        }

    }

    super.onMeasure(widthMeasureSpec,MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY));

    }

这个测量方法是从

g401946949

的博客里看到的,但是显示时,viewPager的高度一直是最大高度,显示的高度与fragment不匹配,所以还是

推荐直接在xml中给ScrollView添加:android:fillViewport="true"属性;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值