viewpager根据每一个fragment高度自适应

效果如下
viewpager根据每一个fragment高度自适应_第1张图片
使用tablayout+viewpager时,根据viewpager自适应fragment里面页面的高度,某一个fragment数据很多,页面很长,viewpager就很长,某一个fragment数据很少,页面很短,viewpager就很短,关键是viewpager,下面是自适应高度的viewpager代码

public class AutoHeightViewPager extends ViewPager {
    private int currentIndex;
    private int height;
    private HashMap<Integer, View> mChildrenViews = new LinkedHashMap<Integer, View>();
    public AutoHeightViewPager(@NonNull Context context) {
        super(context);
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mChildrenViews.size() > currentIndex){
            View child = mChildrenViews.get(currentIndex);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            height = child.getMeasuredHeight();
        }
        if (mChildrenViews.size() != 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 重新设置高度
     *在viewpager的onPageSelected事件调用,选中某个fragment后,重新计算高度
     * @param current
     */
    public void resetHeight(int current) {
        currentIndex = current;
        if (mChildrenViews.size() > current) {
            ViewGroup.LayoutParams layoutParams =  getLayoutParams();
            if (layoutParams == null) {
                layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
            } else {
                layoutParams.height = height;
            }
            setLayoutParams(layoutParams);
        }
    }

    /**
     * 保存View对应的索引,需要自适应高度的一定要设置这个
     * fragment里调用这个方法,把view和位置传进来
     */
    public void setViewForPosition(View view, int position) {
        mChildrenViews.put(position, view);
    }
}

Demo在github

你可能感兴趣的:(Android)