0

亲爱的,我正在实施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();
}
4

1 回答 1

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 );
}
于 2012-11-22T10:26:35.540 回答