Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] Unable to set initial CarouselView item using ScrollTo() in page constructor #7268

Closed
Magendanz opened this issue Aug 24, 2019 · 13 comments
Labels
a/carouselview e/2 🕑 2 help wanted We welcome community contributions to any issue, but these might be a good place to start! t/bug 🐛 up-for-grabs We welcome community contributions to any issue, but these might be a good place to start!

Comments

@Magendanz
Copy link

Magendanz commented Aug 24, 2019

Description

A common practice when initializing a CarouselView is to assign the ItemsSource and then set the initial selection with ScrollTo(object item) in the page constructor. This doesn't work.

Steps to Reproduce

  1. In the page constructor, initialize the CarouselView as follows:
InitializeComponent();

Carousel.ItemsSource = collection;
Carousel.ScrollTo(item);

...where Carousel is the x:Name assigned in XAML.

  1. Track SelectionChanged events with the following:
Carousel.Scrolled += (s, e) => Title = collection[e.CenterItemIndex]?.Name;
  1. See which items get selected.

Expected Behavior

The ScrollTo() call should set the initial selected item to be item.

Actual Behavior

The first item in the collection ends up being selected and the ScrollTo() is ignored.

Basic Information

The workaround is to place the ScrollTo() call in an Appearing event handler. This, however, causes a messy sequence of selections, starting with the first item in the colleciton, followed by the second item, and then finally the target item. Animations make this very distracting.

  • Version with issue: v4.2.0.709249
  • Last known good version: N/A
  • IDE: VS 2019 v16.2.3
  • Platform Target Frameworks:
    • iOS: Untested
    • Android: 8.1 (API 27)
  • Android Support Library Version: 9.4.1.0

Note that this would all be much simpler if CarouselView inherited from SelectedItemsView so that we could just bind the SelectedItem to a property in our ViewModel.

@Magendanz Magendanz added s/unverified New report that has yet to be verified t/bug 🐛 labels Aug 24, 2019
@Magendanz Magendanz changed the title [Bug] Unable to set initial item using ScrollTo() in CarouselView constructor [Bug] Unable to set initial CarouselView item using ScrollTo() in page constructor Aug 24, 2019
@rmarinho
Copy link
Member

rmarinho commented Aug 29, 2019

We are still working on CarouselView, but you just need to set the Position no need to call ScrollTo, Will that work for you?

Carousel.ItemsSource = collection;
Carousel.Position = 2;

@rmarinho rmarinho added the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Aug 29, 2019
@Magendanz
Copy link
Author

I'm not seeing Carousel.Position in current builds, but I honestly wouldn't think to look for it. I'd expect SelectedIndex and/or SelectedItem like in CarouselPage, TabbedPage, Picker and ListView. Does any other control have a Position property?

In fact, SelectedItem is much more useful for the MVVM design pattern.

@rmarinho
Copy link
Member

Hi there's a Position and CurrentItem it works fine with MVVM patterns..

@Julingska
Copy link

Julingska commented Aug 30, 2019

Hi,

I don't have a Position and CurrentItem either and I'm at the latest Xamarin Version thats available for me. 4.2.0.709249 at the moment and there is no update.

I'm having the same problem with the ScrollTo() when initializing the component.

@pauldipietro pauldipietro added this to the 4.3.0 milestone Sep 5, 2019
@samhouts
Copy link
Member

@Julingska Please try 4.3.0-pre1. Thank you!

@Magendanz
Copy link
Author

Bug is still present in 4.3.0.778476-pre1.

@Magendanz
Copy link
Author

Magendanz commented Oct 6, 2019

@samhouts @Julingska : Bug still present in 4.3.0.851321-pre3 when using ScrollTo(). However, you can now use CurrentItem successfully in the constructor.

@samhouts samhouts reopened this Oct 7, 2019
@samhouts
Copy link
Member

samhouts commented Oct 7, 2019

@Magendanz Can you please attach a small project that demonstrates this issue? Thanks!

@samhouts samhouts added the s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. label Oct 7, 2019
@Magendanz
Copy link
Author

Are you unable to reproduce? I thought the code segments above were pretty clear.

@jfversluis
Copy link
Member

Taken the liberty to create a reproduction based on the code provided. Does indeed not seem to work while setting Position does. Both on iOS and Android.

Repro7268.zip

@jfversluis jfversluis removed s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. labels Oct 8, 2019
@jfversluis jfversluis added e/2 🕑 2 and removed s/unverified New report that has yet to be verified labels Oct 8, 2019
@Magendanz
Copy link
Author

So, I think the underlying problem here is with animation. This is on by default, so just calling ScrollTo(item) will attempt to animate to the target item. For me this usually fails.

However, if you call with the animate parameter set to false, it works as expected.

@samhouts samhouts modified the milestones: 4.3.0, 4.4.0 Oct 18, 2019
@samhouts samhouts removed this from the 4.4.0 milestone Feb 11, 2020
@samhouts samhouts added inactive Issue is older than 6 months and needs to be retested help wanted We welcome community contributions to any issue, but these might be a good place to start! up-for-grabs We welcome community contributions to any issue, but these might be a good place to start! and removed inactive Issue is older than 6 months and needs to be retested help wanted We welcome community contributions to any issue, but these might be a good place to start! labels Jul 17, 2020
@samhouts samhouts added this to the 5.0.0 milestone Aug 13, 2020
@samhouts samhouts added inactive Issue is older than 6 months and needs to be retested help wanted We welcome community contributions to any issue, but these might be a good place to start! and removed inactive Issue is older than 6 months and needs to be retested labels Sep 18, 2020
@samhouts samhouts removed this from the 5.0.0 milestone Nov 2, 2020
@rmarinho
Copy link
Member

For this case you should just set the Position.

MonkeyselView.Position = 1;

@Magendanz
Copy link
Author

Well, the workaround is to set the CurrentItem and ensure IsScrollAnimated="False", but that's gotten me unblocked for the past year.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/carouselview e/2 🕑 2 help wanted We welcome community contributions to any issue, but these might be a good place to start! t/bug 🐛 up-for-grabs We welcome community contributions to any issue, but these might be a good place to start!
Projects
None yet
Development

No branches or pull requests

7 participants