Android NestedScrollView 嵌套ViewPager不显示和出现空白部分 解决方法

1、不显示问题–重写ViewPager

public class MyViewPager extends ViewPager {

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

    public MyViewPager(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);
    }
}

2、不显示+空白问题—同样的重写Viewpager

public class ViewPagerForScrollView extends ViewPager{
    private int current;
    private int height = 0;
    /**
     * 保存position与对于的View(保存view对应的索引)
     */
    private HashMap<Integer, View> mChildrenViews = new LinkedHashMap<Integer, View>();

    private boolean scrollble = true;

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (mChildrenViews.size() > current) {
            View child = mChildrenViews.get(current);
            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);

    }

    /**
     * 重新设置高度
     *
     * @param current
     */
    public void resetHeight(int current) {
        this.current = current;
        if (mChildrenViews.size() > current) {
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
                if (layoutParams == null) {
                    layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
                } else {
                    layoutParams.height = height;
                }
                setLayoutParams(layoutParams);
            }
    }
    /**
     * 保存position与对于的View(保存View对应的索引),需要自适应高度的一定要设置这个
     */
    public void setObjectForPosition(View view, int position)
    {
        mChildrenViews.put(position, view);
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (!scrollble) {
            return true;
        }
        return super.onTouchEvent(ev);
    }


    public boolean isScrollble() {
        return scrollble;
    }

    public void setScrollble(boolean scrollble) {
        this.scrollble = scrollble;
    }
}
调用:

Android NestedScrollView 嵌套ViewPager不显示和出现空白部分 解决方法_第1张图片

fragment需要这样写

Android NestedScrollView 嵌套ViewPager不显示和出现空白部分 解决方法_第2张图片

Activity需要这样写

Android NestedScrollView 嵌套ViewPager不显示和出现空白部分 解决方法_第3张图片

Android NestedScrollView 嵌套ViewPager不显示和出现空白部分 解决方法_第4张图片

参考:https://blog.csdn.net/qq_27400335/article/details/81116982

你可能感兴趣的:(#,ViewPager,相关的使用,#,android)