SlidingDrawer从左到右发布

时间:2012-11-15 16:40:49

标签: android slidingdrawer

亲爱的我正在实施ALESSANDRO CRUGNOLA自定义滑动抽屉。我希望它滑到屏幕的一半。我很容易为右到左做这个,但现在我想要从左到右。问题是,当第一个抽屉打开时,它一直滑到右边,然后快速回到我指定的中间点。关于如何解决这个问题的任何想法?指定中间点的代码位于下方,布尔值mInvert对于“从左到右”滑块为真。

@Override
protected void onLayout( boolean changed, int l, int t, int r, int b )
{
    if ( mTracking ) { return; }

    final int width = r - l;
    final int height = b - t;

    final View handle = mHandle;

    int handleWidth = handle.getMeasuredWidth();
    int handleHeight = handle.getMeasuredHeight();

    Log.d( LOG_TAG, "handleHeight: " + handleHeight );

    int handleLeft;
    int handleTop;

    final View content = mContent;
    //mTopOffset = getWidth()/2;
    if ( mVertical ) {
        handleLeft = ( width - handleWidth ) / 2;
        if ( mInvert ) {
            Log.d( LOG_TAG, "content.layout(1)" );
            handleTop = mExpanded ? height - mBottomOffset - handleHeight : mTopOffset;
            content.layout( 0, mTopOffset, content.getMeasuredWidth(), mTopOffset + content.getMeasuredHeight() );
        } else {
            handleTop = mExpanded ? mTopOffset : height - handleHeight + mBottomOffset;
            content.layout( 0, mTopOffset + handleHeight, content.getMeasuredWidth(), mTopOffset + handleHeight + content.getMeasuredHeight() );
        }
    } else {
        handleTop = ( height - handleHeight ) / 2;//centre alings the handle
        if( mInvert ) {
            mBottomOffset = getWidth()/2;//to limit the window sliding half way
            handleLeft = mExpanded ? width - mBottomOffset - handleWidth : mTopOffset;

            content.layout( mTopOffset, 0, mTopOffset + content.getMeasuredWidth(), content.getMeasuredHeight() );
        } else {
            mTopOffset = getWidth()/2;//to limit the window sliding half way
            handleLeft = mExpanded ? mTopOffset : width - handleWidth + mBottomOffset;
            content.layout( mTopOffset + handleWidth, 0, mTopOffset + handleWidth + content.getMeasuredWidth(), content.getMeasuredHeight() );

        }
    }

    handle.layout( handleLeft, handleTop, handleLeft + handleWidth, handleTop + handleHeight );
    mHandleHeight = handle.getHeight();
    mHandleWidth = handle.getWidth();
}

1 个答案:

答案 0 :(得分:0)

我想我会回答这个问题。关键位实际上在onMeasure()中,你必须声明父级是这样的半宽 widthSpecSize / = 2; 当mInvert为真时。

@Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec )
{
    int widthSpecMode = MeasureSpec.getMode( widthMeasureSpec );
    int widthSpecSize = MeasureSpec.getSize( widthMeasureSpec );

    int heightSpecMode = MeasureSpec.getMode( heightMeasureSpec );
    int heightSpecSize = MeasureSpec.getSize( heightMeasureSpec );

    if ( widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED ) { throw new RuntimeException(
            "SlidingDrawer cannot have UNSPECIFIED dimensions" ); }

    final View handle = mHandle;
    measureChild( handle, widthMeasureSpec, heightMeasureSpec );

    if ( mVertical ) {
        int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset;
        mContent.measure( MeasureSpec.makeMeasureSpec( widthSpecSize, MeasureSpec.EXACTLY ), MeasureSpec.makeMeasureSpec( height, MeasureSpec.EXACTLY ) );
    } else {
        int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset;
        if( mInvert ) {
            widthSpecSize /=2;//to limit the sliding halfway parent is told to be halfscreen
            int widthI = widthSpecSize - handle.getMeasuredWidth() - mBottomOffset;
            mContent.measure( MeasureSpec.makeMeasureSpec( widthI, MeasureSpec.EXACTLY ), MeasureSpec.makeMeasureSpec( heightSpecSize, MeasureSpec.EXACTLY ) );
        }else{
            mContent.measure( MeasureSpec.makeMeasureSpec( width, MeasureSpec.EXACTLY ), MeasureSpec.makeMeasureSpec( heightSpecSize, MeasureSpec.EXACTLY ) );
        }
    }

    setMeasuredDimension( widthSpecSize, heightSpecSize );
}