简体   繁体   English

用户以Listview Xamarin形式关注哪个项目

[英]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. 我是一名学习xamarin表格的学生,我试图以此创建一个基本的聊天应用程序,我想知道如何获取当前项目在用户正在观看的listview中的位置。 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. ListView支持一次选择一项。 Selection is on by default. 默认情况下,选择处于打开状态。 When a user taps an item, two events are fired: ItemTapped and ItemSelected. 当用户点击一个项目时,将触发两个事件:ItemTapped和ItemSelected。 Note that tapping the same item twice will not fire multiple ItemSelected events, but will fire multiple ItemTapped events. 请注意,两次轻按同一项目不会触发多个ItemSelected事件,但会触发多个ItemTapped事件。 Also note that ItemSelected will be called if an item is deselected. 另请注意,如果取消选择项目,则将调用ItemSelected。

To detect selecting an item, you can add a method, onSelection: 要检测选择项目,可以添加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: 要禁用选择,只需将selectedItem设置为null即可:

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

you get the selected item from the Xamarin.Forms.ListView.SelectedItem property of your ListView. 您可以从ListView的Xamarin.Forms.ListView.SelectedItem属性中获取选定的项目。 If your ListView.ItemSource is of a type that allows using IndexOf you can now do something like 如果您的ListView.ItemSource是允许使用IndexOf的类型,则现在可以执行类似的操作

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. 如果是这样,则ListView具有ItemAppearing事件。 I use it for example to load more data from an websource if the user scrolled through the first 100 items. 例如,如果用户滚动浏览了前100个项目,我将使用它来从Web资源加载更多数据。 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; 如果您确实需要知道是否显示了特定索引,则可以创建一个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. 然后,您将获得一个列表,其中存储了当前显示的项的所有索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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