We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Getting the index of the presented item

Hello,
is there a way to retrieve the index of the item that is currently presented in the SfCarousel control? My need would basically be to select an item at swipe, and the SelectionChanged event is not useful, as it is raised only when an item is actually tapped.

Thanks in advance,
Alessandro

5 Replies

MS Mugundhan Saravanan Syncfusion Team March 20, 2019 01:19 PM UTC

Hi Alessandro Del Sole 
 
We have checked the reported issue "Getting the index of the presented item" and we suggest to use SelectedIndex property to retrieve the index of the selected item. 
We have provided the sample for your reference below. 
 
 
Thanks.
Mugundhan S.
 



AD Alessandro Del Sole March 20, 2019 01:51 PM UTC

Thank you very much for your kind reply and example.

I forgot to mention I would need this to work with the Linear view mode of the Carousel, and this example won't work because the events SelectionChanged and PropertyChanged for SelectedIndex will not be fired until an item is tapped.

I would be happy to get the same result with a horizontal SfListView, by the way. 

Thanks again!


MS Mugundhan Saravanan Syncfusion Team March 21, 2019 12:55 PM UTC

Hi Alessandro Del Sole,

Thanks for your update.

We have checked the reported issue by changing the ViewMode property to Linear in the sample. The behaviour as such, the PropertyChanged for SelectedIndex when the ViewMode is linear and the SelectionChanged event will be fired only when the item is tapped. We recommend you to use the Default ViewMode in order to retrieve the SelectedIndex without tapping.

Please let us know if you have any other concern.

Regards,
Mugundhan S.


AD Alessandro Del Sole March 21, 2019 01:57 PM UTC

I have solved using a SfListView with horizontal orientation, then retrieving the index via the ScrollRows property of the VisualContainer object in the ScrollStateChanged event as follows:

            if (e.ScrollState == ScrollState.Idle || e.ScrollState == ScrollState.Fling)
            {
                var lineIndex = visualContainer.ScrollRows.GetVisibleLineAtPoint(300);
            
Not an elegant solution, but in this case I can predict the point in which a list item becomes visible at scrolling.


DR Dhanasekar R Syncfusion Team March 25, 2019 12:48 PM UTC

Hi Alessandro, 
 
Thanks for the update.  In order to get the Visible items from list view, we recommend you to use DisplayItems property. And then you can get the index from Display items. 
 
Code snippet
 
var items = listView.DataSource.DisplayItems;
List<int> visibleIndex = new List<int>();
for (int i = 0; i < items.Count; i++)
{
    visibleIndex.Add(listView.DataSource.Items.IndexOf(items[i]));
} 
 
Regards, 
Dhanasekar 


Loader.
Live Chat Icon For mobile
Up arrow icon