How to Scroll to Top on Filter of ListView?

djrpascudjrpascu USMember ✭✭

I have a SearchBar which is used to filter a ListView of data. If I scroll to the bottom or middle of the ListView then start entering data in the SearchBar to filter the list, I want to be able to automatically scroll to the top of the ListView so that it shows items at the top as the filter as applied.

I was thinking to use the ScrollTo method of the ListView on the Textchanged of the SearchBar to just ScrollTo first item in the list, but not sure how to get the item object of the ListView. It's an IEnumberable so I can't index it to get say the first item in the list. Am I heading in the right direction or is there a different way to do this?

Answers

  • djrpascudjrpascu USMember ✭✭
    edited June 2018

    So I figured out how to get the first item.

    Listview.ScrollTo(((IList)Listview.ItemsSource)[0], ScrollToPosition.Start, false);

    But this only works if the ListView isn't scrolling. There doesn't seem to be an event on ListView to determine if it's currently scrolling.

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    If your ListView is using Binding, I believe changing the ItemsSource moves it up.

    Lets say you were bound to a ListItems property.

            var items = ListItems.Where(i => i.Contains("searchText"));
            ListItems = items.ToList();
    

    Probably will scroll to the top.

  • djrpascudjrpascu USMember ✭✭

    Hmm, I am binding my ListView to an ObservableRangeCollection. I'm not doing a contains though, but rather I'm doing a SQL query. In both cases though the ItemsSource gets changed. I'm not seeing the ListView scroll to the top. Are you?

    DatabaseIlokanoWords = await App.Database.SearchByIlokanoWord<TOIDPIlokano>(SearchText);
    IlokanoWords.ReplaceRange(new ObservableRangeCollection<TOIDPIlokano>(DatabaseIlokanoWords));
    
  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    Hmmm.... I was sure I had seen that before.

    Sorry if that didn't work, I can't seem to make it happen either.

  • GovaGova USMember ✭✭
    edited July 2018

    You can try this.

    • Here Item is the class which you defined to bind listview.
    • listView.ItemsSource gets all the items bound to your listview.
    • item.FirstOrDefault() will position to the first element of the list.

      var item = listView.ItemsSource as Item;
      listView.ScrollTo(item.FirstOrDefault(), ScrollToPosition.MakeVisible, false);

    Think it may help you :)

  • djrpascudjrpascu USMember ✭✭
    edited July 2018

    Hey, that worked! I needed to cast it as ObservableCollection. I guess I needed to use MakeVisible instead of Start in my original post. Sorry I originally marked your answer as no, post again and I'll mark it as the answer. Thanks!

  • djrpascudjrpascu USMember ✭✭

    Never mind, spoke to soon. Crashes when types in letters too quickly, even if I put it in a try/catch.

    Unhandled Exception:

    Foundation.MonoTouchException: Objective-C exception thrown. Name: NSRangeException Reason: -[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:usingPresentationValues:]: row (0) beyond bounds (0) for section (0).

  • GovaGova USMember ✭✭

    You can try this.

    • Here Item is the class which you defined to bind listview.
    • listView.ItemsSource gets all the items bound to your listview.
    • item.FirstOrDefault() will position to the first element of the list.

      var item = listView.ItemsSource as Item;
      listView.ScrollTo(item.FirstOrDefault(), ScrollToPosition.MakeVisible, false);

    Think it may help you :)

  • GovaGova USMember ✭✭
    edited July 2018

    @djrpascu said:
    Never mind, spoke to soon. Crashes when types in letters too quickly, even if I put it in a try/catch.

    Unhandled Exception:

    Foundation.MonoTouchException: Objective-C exception thrown. Name: NSRangeException Reason: -[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:usingPresentationValues:]: row (0) beyond bounds (0) for section (0).

    Please refer this link. It may help you.

    https://stackoverflow.com/questions/26647387/uitableview-contentoffsetforscrollingtorowatindexpathatscrollposition

    also this.

    https://stackoverflow.com/questions/26034814/xcode-uitableview-contentoffsetforscrollingtorowatindexpathatscrollposition/26035210

Sign In or Register to comment.