[英]ListView focus on the last item in the list? - Xamarin Forms
Could someone tell me how to keep the ListView
focus always on the last list item? 有人可以告诉我如何使
ListView
始终保持在最后一个列表项上吗? Following the example of a conversation in WhatsApp, where when we send a message, the message that has just arrived is always focused ... or any similar solution? 以WhatsApp中的对话为例,当我们发送消息时,刚到达的消息总是集中在……或任何类似的解决方案上?
What I look for is whenever something is inserted in the ListView
, instead of the Give the option to go up to view the items below, I would like it to be the other way around. 我要寻找的是什么时候将什么插入
ListView
,而不是使用Give the option来查看下面的项目,我希望它是相反的方式。 Always displaying the last item on the screen. 始终在屏幕上显示最后一项。
I created an example where you have two elements, a list view and a buttom for adding new elements. 我创建了一个示例,其中有两个元素,一个列表视图和一个用于添加新元素的按钮。
This is my XAML: 这是我的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>
As you can see the button is very huge in order to show better the scroll. 如您所见,该按钮非常大,以便更好地显示滚动条。
In my Code-behind, I use ListView.ScrollTo
method to scroll to the last added element, with animation: 在我的隐藏代码中,我使用
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);
}
}
I hope that this can help you. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.