【问题标题】:ListView focus on the last item in the list? - Xamarin FormsListView 关注列表中的最后一项? - Xamarin 形式
【发布时间】:2017-06-18 22:56:52
【问题描述】:

有人能告诉我如何让ListView 始终关注最后一个列表项吗?以 WhatsApp 中的对话为例,当我们发送消息时,刚刚到达的消息始终是焦点……或任何类似的解决方案?

我寻找的是每当在ListView 中插入某些东西时,而不是提供选项以查看下面的项目,我希望它是相反的方式。始终在屏幕上显示最后一项。

【问题讨论】:

    标签: c# xaml listview xamarin xamarin.forms


    【解决方案1】:

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

    这是我的 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);
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 谢谢我的朋友!成功!工作完美!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多