android-discuss icon indicating copy to clipboard operation
android-discuss copied to clipboard

scrollview嵌套viewpager,自适应高度如何解决?

Open tianshaokai opened this issue 7 years ago • 1 comments

我已经重写了viewpager

@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);
}

重写这个方法后,切换页面后 页面因为数据个数不一样,而出现大面积空白,不能适应数据多少而自适应, 该如何解决呢??

tianshaokai avatar Dec 18 '17 02:12 tianshaokai

固定viewpage高度,然后处理viewpage和scrollview滑动冲突,推荐在scrollview中处理

    private float mDownPosX, mDownPosY;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final float x = ev.getX();
        final float y = ev.getY();

        final int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mDownPosX = x;
                mDownPosY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                final float deltaX = Math.abs(x - mDownPosX);
                final float deltaY = Math.abs(y - mDownPosY);
                if (deltaX > deltaY) {
                    return false;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }

doutuifei avatar Aug 30 '18 02:08 doutuifei