简体   繁体   中英

Which Item is user focused in listview xamarin forms

I am a student learning xamarin forms, I am trying to create a basic chat app in this I want to know how to get position of current item in listview that's user watching. When a new message received i want to know if user is at bottom or not if at bottom focus the new and if not at the bottom then just add not by adding focus to it.

From the Documentation

ListView supports selection of one item at a time. Selection is on by default. When a user taps an item, two events are fired: ItemTapped and ItemSelected. Note that tapping the same item twice will not fire multiple ItemSelected events, but will fire multiple ItemTapped events. Also note that ItemSelected will be called if an item is deselected.

To detect selecting an item, you can add a method, onSelection:

void OnSelection (object sender, SelectedItemChangedEventArgs e)
{
  if (e.SelectedItem == null) {
    return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
  }
  DisplayAlert ("Item Selected", e.SelectedItem.ToString (), "Ok");
  //((ListView)sender).SelectedItem = null; //uncomment line if you want to disable the visual selection state.
}

To disable selection just set the selectedItem to null:

SelectionDemoList.ItemSelected += (sender, e) => {
    ((ListView)sender).SelectedItem = null;
};

you get the selected item from the Xamarin.Forms.ListView.SelectedItem property of your ListView. If your ListView.ItemSource is of a type that allows using IndexOf you can now do something like

int position = (yourlistview.ItemSource as ObservableCollection<your type>).IndexOf(yourlistview.SelectedItem)

Update:

ok I think i understood what you want. In most cases more than one item is currently shown when using a listview. So their exists not a single index but i think you just want to know if the last item of the list is visible/the user has scrolled to the end?

If so ListView has an ItemAppearing event. I use it for example to load more data from an websource if the user scrolled through the first 100 items. You could do something like this

         listview.ItemAppearing += listviewItemAppearing;
         listview.ItemDisappearing += listviewItemDisappearing;

         bool m_scrolledToEnd;

        private void listviewItemDisappearing(object sender, ItemVisibilityEventArgs e)
        {
            if(e.Item == yourlastiem) 
              m_scrolledToEnd = false;
        }

        private void listviewItemAppearing(object sender, ItemVisibilityEventArgs e)
        {
            if(e.Item == yourlastiem) 
              m_scrolledToEnd = true;
        }

if you realy need to know if a specific index is shown you could create a List<int> m_idxlist; and in the appearing event add the index of the item to the list and in the disappearing event remove the index of the item from the list. Then you will have a list where all indexes of the items currently shown are stored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM