android ViewPager 自适应高度

很多Android开发者会遇到的问题,ViewPager无法自适应内容的高度
下面根据内容动态设置ViewPager的高度,总结给出2种方式:

第一种 :
取所有view中最大高度作为ViewPager的高度,此方法试用所有page页高度相差不多的时候,否则下面会有空白的部分,代码如下:

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" />

第二种
在遇到不是所有界面高度一样的情况下,会导致高度低的页面中出现空白,下面就介绍第二种方式动态设置高度:

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" />
  1. 代码中调用
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) {

            }
        });
 
  1. 设置offset属性,预先加载全部view,防止下面的方法获取到没有加载的view是高度为0
    // View view = getChildAt(getCurrentItem());
// 如果不设置,可能第三个页面以后就显示不出来了,因为offset就是默认值1了
mViewPager.setOffscreenPageLimit(adapter.getCount());

以上就是总结的2种ViewPager自适应高度的方式,希望对你有帮助,不足之处还望大佬指教

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页