ScrollView嵌套listView和Viewpager后的显示不全解决

由于项目要求,需要在Viewpager中有一个是片段是ScrollView,而scrollView中需要嵌套另外的ViewPager跟其他如listView的视图,这里先解决下scrollView中嵌套ViewPager与listView后显示不全的问题。

xml布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <ScrollView
        android:id="@+id/sv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >


            <com.example.scrollviewandother.MyListView
                android:id="@+id/lv_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.example.scrollviewandother.MyListView>


            <com.example.scrollviewandother.MyViewPager
                android:id="@+id/vp_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.example.scrollviewandother.MyViewPager>
        </LinearLayout>
    </ScrollView>


</LinearLayout>

自定义listView,重写他的onMeasure方法:解决listView显示不全


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}


自定义ViewPager,重写他的onMeasure方法:解决viewPager显示不全



@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int viewHeight = 0;
View childView = getChildAt(getCurrentItem());
if(childView!=null){
childView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
viewHeight = childView.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(viewHeight, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}


同时设置Viewpager监听:否则最后一页的数据高度不全


final int currentSelectedPosition = viewpager.getCurrentItem();
viewpager.setOnPageChangeListener(new OnPageChangeListener() {


@Override
public void onPageSelected(int arg0) {
View view = viewpager.getChildAt(currentSelectedPosition);
int height = view.getMeasuredHeight();
LayoutParams layoutParams = (LinearLayout.LayoutParams) viewpager.getLayoutParams();
layoutParams.height = height;
viewpager.setLayoutParams(layoutParams);
}

运行效果;

为了简易所以片段跟activity都用的同一xml,上面的是listView,下面的是viewpager


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值