Java实现拖拽列表项的排序功能

作者:一只菜鸡 时间:2023-11-28 23:39:00 

在一些允许用户自定义栏目顺序的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] + " ";
     }
   }
 }

这样,重新排序后,点击按钮,我们即可观察到结果了。

③把冰箱门关上

把大象(?)装进去之后,最后就是我们的收尾工作了。显然,刚才的列表只是一个中间的载体,是我们待排序栏目的简单显示。一般而言,这个listview会安置在contentdialog或是popup里,那么怎么在重排后立即让父页面上的栏目得到相应,进行重排呢?我们用个预定义的委托即可,加在刚才的后台代码里(冰箱能装的东西其实挺多的)。


public Action action;

然后在父页面注册方法,比如:           


btn.Click += async (s, e) =>
      {
        var dialog = new Dialogs.Sort();
        dialog.action += async () => { await sortagain(); };
        await dialog.ShowAsync();
     };

以上所述是小编给大家介绍的Java实现拖拽列表项的排序功能网站的支持!

来源:http://www.cnblogs.com/DaweiX/p/6427637.html

标签:java,拖拽排序
0
投稿

猜你喜欢

  • 快速解决Android适配底部返回键等虚拟键盘的问题

    2021-10-25 14:50:23
  • 阿里面试Nacos配置中心交互模型是push还是pull原理解析

    2023-04-09 06:40:54
  • C#中缓存System.Web.Caching用法总结

    2021-09-05 04:41:11
  • 深入了解Java中的反射机制(reflect)

    2022-06-22 06:34:18
  • java中Class.getMethods()和Class.getDeclaredMethods()方法的区别

    2021-05-28 23:05:46
  • 浅谈c#中config.exe 引发的一些问题

    2022-10-20 07:11:07
  • C#实现为类和函数代码自动添加版权注释信息的方法

    2021-11-30 10:05:21
  • springboot依赖冲突问题及解决过程

    2023-03-13 11:49:53
  • Android adb logcat 命令查看日志详细介绍

    2022-10-28 07:42:44
  • java异常(Exception)处理机制详解

    2023-06-06 08:21:48
  • C# using语法糖图文详解

    2023-08-05 02:57:36
  • spring中bean id相同引发故障的分析与解决

    2023-08-05 11:30:41
  • 盘点MQ中的异常测试

    2022-05-06 07:39:24
  • IDEA集成JProfiler11可视化工具的详细流程(安装、集成、测试)

    2021-12-12 04:05:35
  • tcp socket客户端和服务端示例分享

    2021-10-22 13:24:17
  • Java实现XML文件学生通讯录

    2023-07-23 19:21:49
  • 在Android中查看当前Activity是否销毁的操作

    2023-02-26 03:58:21
  • java开发中遇到的异常汇总详解

    2023-03-21 02:54:15
  • Unity 通过LineRenderer绘制两点之间的直线操作

    2021-08-04 04:15:08
  • Java语法基础之运算符学习笔记分享

    2021-12-18 02:13:17
  • asp之家 软件编程 m.aspxhome.com