2012-11-15 172 views
0

尊敬的各位,我正在實施ALESSANDRO CRUGNOLA定製滑動抽屜。我希望它滑過屏幕一半。我很輕易就做到了這一點,但現在我想從左到右。問題是,當抽屜第一次打開時,它一直滑到右側,然後回到我指定的中間點。關於如何解決這個問題的任何想法?用於指定中途點的代碼位於下方,左側至右側滑塊的布爾mInvert爲true。SlidingDrawer從左至右發行

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

回答

0

我以爲我會回答這個問題。關鍵的一點實際上是在onMeasure()中,你必須聲明它的父寬是這樣的寬度,如下所示: widthSpecSize/= 2; mInvert爲true時爲 。

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