Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Restore relative position of Views after Orientation Change

$
0
0

I'm trying to create a "range slider".
What I have is 2 simple views, that represent the two thumbs and a frame layout that represents the slider.

I'm laying out my two thumbs in the overriden OnLayout method, based on the width of the slider multiplied by the value of a thumb (between 0 and 1).

protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
    if (changed)
    {
        //Get the width and height if not set yet
        if (ThumbLeft.MeasuredWidth == 0 || ThumbLeft.MeasuredHeight == 0)
            ThumbLeft.Measure(MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified), MeasureSpec.MakeMeasureSpec(b, MeasureSpecMode.AtMost));

        if (ThumbRight.MeasuredWidth == 0 || ThumbRight.MeasuredHeight == 0)
            ThumbRight.Measure(MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified), MeasureSpec.MakeMeasureSpec(b, MeasureSpecMode.AtMost));

        //calculate width and the relative position of the thumbs
        int width = r - l;
        int thumbLeftX = (int)(width * lastProgressLeft); //last progresses are a value between 0 and 1
        int thumbRightX = (int)(width * lastProgressRight);

        //position the thumbs
        ThumbLeft.Layout(l: l + thumbLeftX,
                         t: t,
                         r: l + thumbLeftX + ThumbLeft.MeasuredWidth,
                         b: ThumbLeft.MeasuredHeight);
        ThumbRight.Layout(l: l + thumbRightX - ThumbRight.MeasuredWidth,
                          t: t,
                          r: l + thumbRightX,
                          b: ThumbLeft.MeasuredHeight);
    }
}

The values really seem to be correct, in landscape, the "r" value is bigger than in portrait. "l" is always 0 since the control is aligned to the left of my screen. The calculation seems correct, I tested moving one of the thumbs to the middle, so I can exactly see if thumbLeftX or thumbRightX are 50% of the width. It seemed correct.

I think the Layout(...) calls on the thumbs do not layout my thumbs reliably.
Is there a layout call that im missing?
Do I need to call other methods to relayout my thumbs correclty?

Initially the lastProgressLeft is 0 and the lastProgressRight is 1 (meaning thumb left should be at the left end of the slider, the right thumb at the right end of the slider. This works great, also on orientation changes it looks correct.

BTW: This is happening as a control of a custom renderer (SetNativeControl(thisRangeSlider)). There might be some xamarin layout invocations that intercept my logic?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>