由于项目要求,需要在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