需要解决的问题:
ViewPager高度为0
ViewPager的高度,需要统一为所有Item中,最高的那个
解决方案:
自定义ViewPager类,并继承ViewPager并覆盖onMeasure方法,手动计算高度
遍历子布局,取得最大高度,并设置为ViewPager高度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) { int maxHeight = -1 ; int count = getAdapter().getCount(); for (int i = 0 ; i < count; i++) { Object o = getAdapter().instantiateItem(this , i); if (o instanceof View) { ((View) o).measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0 , MeasureSpec.UNSPECIFIED)); int measuredHeight = ((View) o).getMeasuredHeight(); if (measuredHeight > maxHeight) { maxHeight = measuredHeight; } } } if (maxHeight != -1 ) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.EXACTLY); } super .onMeasure(widthMeasureSpec, heightMeasureSpec); }
这样,解决了显示高度的问题,但是在嵌套的情况下,会出现ViewPager无法滑动,或只有部分区域可以滑动,所以遇到了下面的问题。
解决方法:
在刚才自定义的ViewPager类中,重写onTouchEvent方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 @Override public boolean onTouchEvent (MotionEvent ev) { if (getChildCount() <= 1 ) { super .onTouchEvent(ev); } switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: if (getParent() != null ) { getParent().requestDisallowInterceptTouchEvent(true ); } break ; case MotionEvent.ACTION_MOVE: if (getParent() != null ) { getParent().requestDisallowInterceptTouchEvent(true ); } break ; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (getParent() != null ) { getParent().requestDisallowInterceptTouchEvent(true ); } break ; } super .onTouchEvent(ev); return true ; }
这样,滑动事件就可以正常响应了。
最后,补充一个不使用Fragment,使用View来填充的ViewPager实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 class ViewPagerAdapter extends PagerAdapter { private Context mContext; private List<View> mViewList; private String mData; public ViewPagerAdapter (Context mContext, List<View> mViewList) { this .mContext = mContext; this .mViewList = mViewList; } @Override public int getCount () { return mViewList != null ? mViewList.size() : 0 ; } @NonNull @Override public Object instantiateItem (@NonNull ViewGroup container, int position) { View view = mViewList.get(position); if (view.getParent() != null ) { View parent = (View) view.getParent(); if (parent instanceof ViewGroup){ ((ViewGroup) parent).removeView(view); } } container.addView(view); return view; } @Override public void destroyItem (@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } @Override public boolean isViewFromObject (@NonNull View view, @NonNull Object object) { return view == object; } public void setData (String strData) { this .mData = strData; for (View view : mViewList) { if (mData != null ) { if (view instanceof ChildView) { ((Child) view).setData(strData); } } } } }