Search code examples
androidslidingdrawer

Dynamically change SlidingDrawer content width


I'm currently extending the SlidingDrawer class and want to resize the width of the content and can't quite get it to work right. Right now I'm setting the width of the whole view (handle and content) and it works for resizing purposes but also introduced a visual glitch when I move the handle it jumps to the new size for a split second and then returns to the handles position. I'm thinking that the problem is stemming from the onMeasure() or onLayout() calls that are happening in the base SlidingDrawer that are preventing the content area to be resized but am not completely sure.

I'm using getLayoutParams().width = newWidth; to resize the whole view but would like to use something like mContent.getLayoutParams().width = newWidth;.

The source code for the onMeasure() is here and for the onLayout() here.

Any insight into why the content area can't be resized would be great. Thanks!


Solution

  • So I finally figured it out if anyone else was having an issue with this. Basically when you want to resize the layout you need to measure() the layouts after the size change. Without the offsetLeftAndRight() call the handle will "jump" to the new size for a split second so setting the offset eliminates that "jump".

    A simplified version of what I did was essentially:

    public void resize() {
       int previousPosition = mHandle.getLeft();
    
       //Set the new size of the content area
       mContent.getLayoutParams().width = width;
    
       //Measure the newly sized content area and adjust the layout
       mContent.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(getBottom() - getTop(), MeasureSpec.EXACTLY));
       mContent.layout(handleWidth + mTopOffset, 0, mTopOffset + handleWidth + content.getMeasuredWidth(), content.getMeasuredHeight());
    
       /* Remeasure any other views that were resized also here */
    
       //Not required but helps position the handle correctly
       mHandle.offsetLeftAndRight(previousPosition);
    }