1. 程式人生 > >android ViewPager 自適應高度

android ViewPager 自適應高度

很多Android開發者會遇到的問題,ViewPager無法自適應內容的高度,包括子Fragment或view
下面根據需求給出2中方式來動態根據內容設定ViewPager的高度:

1.是網上大家都說的取所有view最大高度作為ViewPager的高度程式碼如下:

public class AutoHeightViewPager extends ViewPager {  

    public AutoHeightViewPager (Context context) {  
        super(context);  
    }  

    public AutoHeightViewPager
(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int height = 0; for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0
, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); if (h > height) height = h; } heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }

使用方法也很簡單,直接在xml佈局裡面替換原來的ViewPager就行:

  xxx.xxx.xxx代表路徑

 <xxx.xxx.xxx.AutoHeightViewPager
    android:id="@+id/viewpager_my"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

2.但是在遇到不是所有介面高度一樣的情況下,會導致高度低的頁面中出現空白,下面就介紹第二種方式動態設定高度:

public class AutoHeightViewPager extends ViewPager {

    public AutoHeightViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // find the current child view
        // and you must cache all the child view
        // use setOffscreenPageLimit(adapter.getCount())
        View view = getChildAt(getCurrentItem());
        if (view != null) {
            // measure the current child view with the specified measure spec
            view.measure(widthMeasureSpec, heightMeasureSpec);
        }

        setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
    }

    /**
     * Determines the height of this view
     *
     * @param measureSpec A measureSpec packed into an int
     * @param view the base view with already measured height
     *
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec, View view) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            // set the height from the base view if available
            if (view != null) {
                result = view.getMeasuredHeight();
            }
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    /**
     * 單獨測量view獲取尺寸
     *
     * @param view
     */
    public void measeureView(View view) {

        int intw = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int inth = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        // 重新測量view
        view.measure(intw, inth);

        // 以上3句可簡寫成下面一句
        //view.measure(0,0);

        // 獲取測量後的view尺寸
        int intwidth = view.getMeasuredWidth();
        int intheight = view.getMeasuredHeight();
    }
}

註釋中有詳細解釋,使用方法如下:

1.xml中配置:
  xxx.xxx.xxx代表路徑

 <xxx.xxx.xxx.AutoHeightViewPager
    android:id="@+id/viewpager_my"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

2.程式碼中呼叫
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(final int position) {
                // 切換到當前頁面,重置高度
               mViewPager.requestLayout();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

3.在這是offset屬性,預先載入全部view(頁面特別多別多的情況可能有效能問題)
    防止 獲取到View view = getChildAt(getCurrentItem());高度為0

// 如果不設定,可能第三個頁面以後就顯示不出來了
mViewPager.setOffscreenPageLimit(adapter.getCount());

以上就是總結的2中ViewPager自適應高度的方式,希望對你有幫助,不足之處還望大佬指教