ScrollView嵌套ViewPager,解决滑动冲突以及ViewPager加载不同内容动态设置高度显示

时间:2024-04-05 11:03:54

1、创建自定义ViewPager

 

public class ViewPagerForScrollView extends ViewPager {

    int myh=0 ;
    boolean ifme =false;

    public ViewPagerForScrollView(Context context) {
        super(context);
        Log.i("lgq","高度==111=  " );
    }

    public ViewPagerForScrollView(Context context, AttributeSet attrs) {

        super(context, attrs);
        Log.i("lgq","高度==22==  " );
        TestObServernotice.getInstance().addObserver(deleteObserver);
    }

    private Observer deleteObserver = new Observer() {
        @Override
        public void update(int id, int intparam, String param, Bitmap bitmap) {
            if (id == 111) {
                myh = intparam;
                ifme = true;
            }
        }
    };



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

        int height = 0;
        if (ifme){

            View child = getChildAt(myh);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            height = h;
        }else {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                int h = child.getMeasuredHeight();
                Log.i("lgq","高度====  " +h);
                if (h > height)
                    height = h;
            }
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
//        heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

2、ScrollView属性

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true">

 

 实现样式

长高度内容                                                                                                                 短高度内容

ScrollView嵌套ViewPager,解决滑动冲突以及ViewPager加载不同内容动态设置高度显示  ScrollView嵌套ViewPager,解决滑动冲突以及ViewPager加载不同内容动态设置高度显示

 

实现demo:https://download.csdn.net/download/meixi_android/12390494

bug交流:qq1085220040