没有合适的资源?快使用搜索试试~ 我知道了~
首页Java实现拖拽列表项的排序功能
资源详情
资源评论
资源推荐

Java实现拖拽列表项的排序功能实现拖拽列表项的排序功能
主要介绍了Java实现拖拽列表项的排序功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
在一些允许用户自定义栏目顺序的app(如:凤凰新闻、网易云音乐等),我们可以方便地拖拽列表项来完成列表的重新排
序,进而完成对栏目顺序的重排。这个功能很人性化,而实现起来其实很简单(甚至都不用写什么后台代码),只有三步。
①把冰箱门打开把冰箱门打开
首先,我们需要让冰箱的大门敞开,也就是允许我们进行拖拽的相关操作。以ListView为例,注意下面几个属性。
<StackPanel>
<ListView x:Name="list"
AllowDrop="True"
CanReorderItems="True"
IsSwipeEnabled="True">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Margin" Value="4"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
<Button Click="Button_Click">Show Items</Button>
<TextBlock x:Name="txt"/>
</StackPanel>
AllowDrop属性允许元素进行拖动,它继承自UIElement基类,为所有可视元素支持。
CanReorderItems属性继承自ListViewBase基类,允许列表控件的项可以重新排序。
IsSwipeEnabled属性(swipe有“轻扫”之意)也需要设置为“True”,否则在触摸屏等输入设备下无法进行操作。相关的详尽说
明在MSDN文档里有介绍(https://docs.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Controls.ListViewBase),此部分摘
录部分原文:
Remarks
Setting IsSwipeEnabled to false disables some default touch interactions, so it should be set to true when these interactions
are needed. For example:
If item selection is enabled and you set IsSwipeEnabled to false, a user can deselect items by right-clicking with the mouse,
but can't deselect an item with touch by using a swipe gesture.
If you set CanDragItems to true and IsSwipeEnabled to false, a user can drag items with the mouse, but not with touch.
If you set CanReorderItems to true and IsSwipeEnabled to false, a user can reorder items with the mouse, but not with
touch.
You typically set IsSwipeEnabled to false to disable swipe animations when items in the view don't support interactions that
use the swipe gesture, like deselecting, dragging, and reordering. Disabling the animation when it's not needed can improve
the performance of your app.
(有趣的是最后一段:当列表不允许轻扫手势(撤销选定,拖动,拖拽重排)时,我们可以“显式”地将IsSwipeEnabled属性设
置为False来提升应用的性能。)
②把大象装进去把大象装进去
前台ok后,我们就可以在后台加点东西,把我们的排序逻辑(其实并没有,微软已经写好了)添加进去。这个demo里,我用
了一个按钮和一个文本框来观察重排的结果。如下:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
for (int i = 0; i < 10; i++)
{
list.Items.Add($"-----THIS IS ITEM {i}-----");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
txt.Text = string.Empty;
foreach (var item in list.Items)
{
txt.Text += item.ToString()[18] + " ";

















安全验证
文档复制为VIP权益,开通VIP直接复制

评论1