1. 程式人生 > >ListView顯示不全,ViewPager顯示不全,ScrollVIew巢狀ViewPager顯示問題

ListView顯示不全,ViewPager顯示不全,ScrollVIew巢狀ViewPager顯示問題

自定義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);
}