How to do AbsoluteLayout for ViewCells with Labels?

MikeLorenzMikeLorenz USMember
edited November 2015 in Xamarin.Forms

Hi all:

TLDR: I'm looking for any sample code for correctly doing...

  • ViewCells in a ListView
  • using AbsoluteLayouts
  • without setting any Proportional bits
  • with uneven rows
  • containing word-wrapping Labels

I have an app that requires ViewCells in a ListView with uneven rows containing word-wrapping Labels. I started with the standard design method, using nested StackLayouts and Grids. Works OK, but on my Android device the scrolling is slow and stuttering. So I switched to using AbsoluteLayouts, following the method Charles Petzold used in Chapter 14: I add a handler for the SizeChanged event & in there I make various AbsoluteLayout.SetLayoutBounds() calls. I calculate the required row size, and set the HeightRequest for that cell accordingly.

All of this works great on my iPhone and my Android 7" tablet. Scrolling is much faster and smoother.

THE PROBLEM is when I run it on an iPad. When the page first appears, the cells are sized oddly, with many cells being taller than they need to be. After the cells scroll off-screen, they come back looking perfect.

The 1st screen shot below shows what the initial page looks like. The 2nd one is after I scrolled everything off-screen and scrolled back up.

I've been struggling with this problem for the better part of a day now, and I'm stumped.

THE REASON this is happening, as best I can determine, is that each cell's SizeChanged event is called twice. The 1st time, the cell's Width is given as 320, so all word wrapping is calculated incorrectly. The 2nd time it gets the correct Width (760+). But apparently only the AbsoluteLayout.SetLayoutBounds calls I make on the 1st pass are displayed. The calls I make on the 2nd pass are what gets used after the cell goes off-screen and comes back on-screen.

Has anyone else been through this? Any help would be much appreciated.

Thanks,
Mike

P.S. - I know I could use custom renderers, but that's the "nuclear" option, and I'd prefer to avoid that if I possibly can. The AbsoluteLayout speed is acceptable to me -- I just need to get the layout working correctly on the 1st pass.

Answers

  • MikeLorenzMikeLorenz USMember
    edited November 2015

    Hi again. I tried some more yesterday, but didn't come up with anything new.

    Has anyone worked with AbsoluteLayout enough to be able to offer some wisdom?

    Thanks!

  • Sadly all I can say is that I have the same problem on iPads.

  • MikeLorenzMikeLorenz USMember
    edited December 2015

    I've attached a zip file containing a complete XF PCL sample project. (The best way to see what's happening is to run it in the iOS simulator for an iPad.)

    @BastiBrauning - Good to know I'm not the only one!

    Thanks,
    Mike

Sign In or Register to comment.