繁体   English   中英

ListView集中在列表的最后一项吗? -Xamarin表格

[英]ListView focus on the last item in the list? - Xamarin Forms

有人可以告诉我如何使ListView始终保持在最后一个列表项上吗? 以WhatsApp中的对话为例,当我们发送消息时,刚到达的消息总是集中在……或任何类似的解决方案上?

我要寻找的是什么时候将什么插入ListView ,而不是使用Give the option来查看下面的项目,我希望它是相反的方式。 始终在屏幕上显示最后一项。

我创建了一个示例,其中有两个元素,一个列表视图和一个用于添加新元素的按钮。

这是我的XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="4*" />
    </Grid.RowDefinitions>
    <ListView x:Name="ListView" />
    <Button
        Grid.Row="1"
        Clicked="Button_OnClicked"
        Text="Add animal" />
</Grid>

如您所见,该按钮非常大,以便更好地显示滚动条。

在我的隐藏代码中,我使用ListView.ScrollTo方法滚动到最后一个带有动画的元素:

public partial class MainPage : ContentPage
{
    private ObservableCollection<String> animals;
    private int _counter = 0;

    public MainPage()
    {
        InitializeComponent();
        animals = new ObservableCollection<string>(new List<string>{"dog","cat","fish","elephant","monkey"});
        ListView.ItemsSource = animals;
    }

    private void Button_OnClicked(object sender, EventArgs e)
    {
        var newElement = "new animal"+_counter;
        _counter++;
        animals.Add(newElement);
        ListView.ScrollTo(newElement, ScrollToPosition.End, true);
    }
}

希望对您有所帮助。

暂无
暂无

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

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