使用DialogFragment 实现嵌套ViewPager,按

2019-06-06  本文已影响0人  则卷滔滔

产品需求:

一个dialog中有个点击显示详情按钮,而详情要是在弹窗内部展示,产品需要的效果是想页面跳转一样有一个向右的动画效果,这个时候就想到了在dialog中嵌套viewpager来完成

碰到的问题:

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

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

    var height = 0
    //下面遍历所有child的高度
    for (i in 0 until childCount) {
        if (i == 0) {
            val child = getChildAt(i)
            child.measure(widthMeasureSpec,
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
            val h = child.measuredHeight
            if (h > height)
            //采用最大的view的高度。
                height = h
        }
    }

    var heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)

    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}

fun setNoScroll(noScroll: Boolean) {

    this.noScroll = noScroll
}

override fun onTouchEvent(arg0: MotionEvent): Boolean {
    return if (noScroll)
        false
    else
        super.onTouchEvent(arg0)
}

override fun onInterceptTouchEvent(arg0: MotionEvent): Boolean {
    return if (noScroll)
        false
    else
        super.onInterceptTouchEvent(arg0)
}
上一篇下一篇

猜你喜欢

热点阅读