Xamarin.Forms ListView Issues [Android]

NunoFariaNunoFaria USMember ✭✭

I'm currently working on a Xamarin.Forms project, and i noticed two issues when working with ListView on Android (i didn't test the other platforms, yet)

1 - When SeparatorVisibility is set to SeparatorVisibility.None, the Separator still occupies space in the List.

When looking into the Xamarin.Forms.Platform.Android.ListViewAdapter.cs(Xamarin.Forms source code) we can see that the Color is being set to Transparent, when it would be useful that the Separator didn't occupy any space at all.

 if (nextCellIsHeader || _listView.SeparatorVisibility == SeparatorVisibility.None)
     bline.SetBackgroundColor(global::Android.Graphics.Color.Transparent);

To fix this i switched the code to:

if (nextCellIsHeader || _listView.SeparatorVisibility == SeparatorVisibility.None)
     bline.Visibility = ViewStates.Gone;

And then i got this (now the Separators are really gone and i can embed my own separators into cells without having that invisible space lying there):

The problem is, this requires to compile Xamarin.Forms.Platform.Android which doesn't seems a very good option since i don't want to customize each new Xamarin.Forms update manually. I also tried to create a custom ListViewAdapter, with no success since i can't access internal stuff.

2 - During OnMeasure the Separator height isn't taken into account causing the ListView to become scrollable by 2 or 3 px when it has enough space to render everything

Some additional info:
-The ListView is inside a Frame which adapts to the ListView size (see the images above).
-RowHeight is set

On Xamarin.Forms.ListView.cs OnMeasure(Xamarin.Forms source code) we have:

request = new Size(width, list.Count * RowHeight);

Most probably, Separators take extra space on the ListView beyond RowHeight, but since that extra space isn't added to the ListView height calculation, the scrollable area becomes bigger than the actual ListView size causing the ListView to become scrollable by a few pixels.

When i applied the fix i came up with to solve issue number 1, the ListView stopped scrolling (beacuse the extra space isn't there anymore - SeparatorVisibility.None must be set). Another fix i came up with consists in subclassing ListView and overriding OnMeasure in order to add a little extra space to accomodate all Separators

request = new Size(width, list.Count * (RowHeight + SeparatorHeight));

Note: I used a fixed size to test this fix but since Separator height is natively set to 1, i think that SeparatorHeight depends on screen density.

I attached a sample project where both issues are present. I also included the modified Xamarin.Forms.Platform.Android.dll in case anyone want to test it (you just have to swap references).

I think it would be interesting to see these fixes on a future release, specially number 1 since it requires messing with the Platform code and it would allow a better control over ListView look.

If anyone knows another way to solve these issues I'd like to hear about it.

Sign In or Register to comment.