android SlidingUpPanel(二)

本文详细探讨了android SlidingUpPanelLayout的工作原理,从构造函数的初始化,onMeasure()方法中的布局测量,到onLayout()中的子View定位。重点介绍了如何设置拖动视图以及实现拖动效果的关键——ViewDragHelper,包括实现DragHelperCallback回调接口,以及重写onInterceptTouchEvent和onTouchEvent方法来传递拖动事件。
摘要由CSDN通过智能技术生成

android SlidingUpPanel(一)中简单介绍了ViewDragHelper以及SlidingUpPanelLayout的基本原理,本篇文章

  根据SlidingUpPanelLayout的基本原理介绍SlidingUpPanelLayout的具体实现。

SlidingUpPanelLayout的构造函数中,从布局文件中获取SlidingUpPanelLayout的一些属性值,初始化成员变量,

其中有如下代码:

mDragHelper = ViewDragHelper.create(this, 0.5f, new DragHelperCallback());

mDragHelper.setMinVelocity(mMinFlingVelocity * density);

通过ViewDragHelper的create方法创建了一个ViewDragHelper实例,并设置它的最小velocity(最小速率)。接着我们看

SlidingUpPanelLayout的onMeasure()方法:

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    

    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);

    final int widthSize = MeasureSpec.getSize(widthMeasureSpec);

    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode != MeasureSpec.EXACTLY) {
    

        throw new IllegalStateException("Width must have an exact value or MATCH_PARENT");

    } else if (heightMode != MeasureSpec.EXACTLY) {
    

        throw new IllegalStateException("Height must have an exact value or MATCH_PARENT");

    }

    final int childCount = getChildCount();

    if (childCount != 2) {
    

        throw new IllegalStateException("Sliding up panel layout must have exactly 2 children!");

    }

    mMainView = getChildAt(0);

    mSlideableView = getChildAt(1);

    if (mDragView == null) {
    

        setDragView(mSlideableView);

    }

    // If the sliding panel is not visible, then put the whole view in the hidden state

    if (mSlideableView.getVisibility() != VISIBLE) {
    

        mSlideState = PanelState.HIDDEN;

    }

    int layoutHeight = heightSize - getPaddingTop() - getPaddingBottom();

    int layoutWidth = widthSize - getPaddingLeft() - getPaddingRight();

    // First pass. Measure based on child LayoutParams width/height.

    for (int i = 0; i < childCount; i++) {
    

        final View child = getChildAt(i);

        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        // We always measure the sliding panel in order to know it's height (needed for show panel)

        if (child.getVisibi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值