Implementing Scroll To Item

3 posts, 0 answers
  1. Duane
    Duane avatar
    2 posts
    Member since:
    May 2014

    Posted 31 Oct Link to this post

    I'm trying to create a customrenderer for the ListView control to allow me to scroll to a specific item in list. I've managed to do this for Android, although I had to do reflection to get at the needed properties. But it works well enough.

    I'm now trying to implement for iOS and am running into a problem finding the NSIndexPath for the selected item.

    I've found the ScrollToItem method on TKListView, but it requires an NSIndexPath. I have access to the actual item from my Xamarin.Forms library, how do I translate that into an NSIndexPath?

  2. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    559 posts

    Posted 03 Nov Link to this post

    Hi, Duane,

    You can get the index of the item through the Xamarin.Forms RadListView's ItemsSource in the custom renderer. For example:

    protected override void OnElementChanged(ElementChangedEventArgs<RadListView> e)
            {
               base.OnElementChanged(e);
     
               var FormsListView = e.NewElement as RadListView;
               var source = FormsListView.ItemsSource as List<SourceItem>;
               SourceItem item = source.Where(x => x.Name == "Test 1").FirstOrDefault();
               int indexOfItem = source.IndexOf(item);
     
               indexPath = NSIndexPath.FromItemSection(indexOfItem, 0);
            }

    Eventually, you can use this indexPath in the ScrollToItem method.

    I have attached a sample for your reference.

    Regards,
    Stefan Nenchev
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Duane
    Duane avatar
    2 posts
    Member since:
    May 2014

    Posted 03 Nov in reply to Stefan Nenchev Link to this post

    Thanks Stefan, I forgot to come back and post that I figured it out. I went down a similar path, once I realized I could create the NSIndexPath it went quickly. I'll post a blog post on my solution and link it here.
Back to Top