Bug 51657 - CarouselView does not reassign ViewPager.Adapter when used within TabbedPage
Summary: CarouselView does not reassign ViewPager.Adapter when used within TabbedPage
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Chris King
Depends on:
Reported: 2017-01-20 10:32 UTC by Josha Munnik
Modified: 2017-05-04 17:09 UTC (History)
6 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:

Source of simple app that uses TabbedPage with CarouselView (2.84 KB, application/x-zip-compressed)
2017-02-17 08:40 UTC, Josha Munnik

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Josha Munnik 2017-01-20 10:32:28 UTC
I've created an app with a TabbedPage with three pages (all ContentPage instances). The first page uses AbsoluteLayout and contains a ContentViewController (with three items) which covers the whole screen and on top a small BoxView. The other two pages just contain a single Label.

When I select the second tab and then go back to the first tab everything works ok.

However when I select the third tab and then go back to the first tab, the carousel view is no longer showing (the BoxView element is still shown).

As extra test I turned on the indicators (ShowIndicators="True") to see if these would show up. Again there was no problem switching between first and second tabs. When I selected the third tab and then went back to the first tab, I got an null pointer exception.

To dig deeper into this I've downloaded the CarouselView source from Github and recreated the pages within the demo project. 

When returning to the first tab from the third tab, the CarouselViewRenderer.OnElementChanged (CarouselView.FormsPlugin.Android project) is called and recreates the viewPager; however because the properties have not changed (I assume), viewPager.Adapter is never assigned a value within CarouselViewRenderer.OnElementPropertyChanged method. This is also what generates the Null pointer exception because the CirclePageIndicator tries to access the Adapter.Count property.

I've changed CarouselViewRenderer.OnElementPropertyChanged and added the following code to create the adapter also when the Renderer property changes:

	protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		base.OnElementPropertyChanged(sender, e);

		switch (e.PropertyName)
       	        case "Renderer":
               	    viewPager.Adapter = new PageAdapter(Element, viewPager);
                    viewPager.SetCurrentItem(Element.Position, false);

I've no idea if the above is the best solution to solve this problem.
Comment 1 Paul DiPietro [MSFT] 2017-02-15 15:55:58 UTC
Could you possibly upload a minimized reproduction to take a look at?
Comment 2 Josha Munnik 2017-02-17 08:40:33 UTC
Created attachment 19859 [details]
Source of simple app that uses TabbedPage with CarouselView

To use source (Visual Studio):
1. Create new project
2. Cross platform -> Blank App (Xamarin Forms Portable)
3. Right click solution -> Manage NU Packages
4. Add CarouselView
5. Unzip source into Forms project folder

The full solution (17mb) can be downloaded from: http://www.ultraforce.com/carousel_test.zip

To see bug:
1. Run app on Android (should show 3 tabs with zoo image and blue box)
2. Select 2nd tab
3. Select 1st tab (zoo image should become visible again)
4. Select 3rd tab
5. Select 1st tab (only the blue box is shown)

App has been tested on Samsung S7

Setting ShowIndicators to True (in TabPage1.xaml) will crash the app when returning to the 1st tab.

The Zoo data (without the monkeys) comes from:
Comment 3 Samantha Houts [MSFT] 2017-03-16 01:02:41 UTC
Thank you!
Comment 4 Chris King 2017-05-04 17:09:08 UTC
CarouselView will be deprecated and replaced with https://github.com/xamarin/Xamarin.Forms/pull/853.