android drag and drop listview,Drag and Drop ListView row reordering

Introduction

An application I worked on required a mechanism whereby the user could rearrange the index of a row within a ListView control at runtime. For this, I wanted to use Drag and Drop, however after trailing through the Internet for some examples, I was unable to find something specific to C#, therefore I decided to implement my own.

Using the code

The ListView control presented here adds the property AllowRowReorder to the existing .NET ListView, which enables or disables the control's ability to reorder it rows.

In order to provide Drag and Drop support to the control, the methods OnItemDrag, OnDragEnter and OnDragDrop of the base control must be overridden. OnItemDrag is the method called once the control detects that a Drag and Drop operation has begun. It is here that any pre 'Drag and Drop' logic must be performed; this entails ensuring that row reordering has been enabled. Finally to inform the ListView that the Drag and Drop operation is valid, the DoDragDrop method is called, the first parameter being the Drag data, which identifies the object to be dragged and needs to be a String, Bitmap or a MetaFile. For the ListView provided, this is a hard-coded string, which will be used in the OnDragEnter method for Drop validation, since the items to be dragged will implicitly be the currently selected ListView items.

C#

Copy Code

protected override void OnItemDrag(ItemDragEventArgs e)

{

base.OnItemDrag(e);

if(!this.AllowRowReorder)

{

return;

}

base.DoDragDrop(REORDER, DragDropEffects.Move);

}

The OnDragEnter validates whether or not the item being dragged, as identified by the DragEventArgs.Data object, can be dropped into the control. Note that when Drag and Drop is enabled within the control, it must provide logic to handle drag events from all drag enabled sources. The ListView provided simply ensures that the data passed matches the hard-coded value supplied by the OnItemDrag method.

C#

Copy Code

protected override void OnDragEnter(DragEventArgs e)

{

base.OnDragEnter(e);

if(!this.AllowRowReorder)

{

e.Effect = DragDropEffects.None;

return;

}

if(!e.Data.GetDataPresent(DataFormats.Text))

{

e.Effect = DragDropEffects.None;

return;

}

String text = (String)e.Data.GetData(REORDER.GetType());

if(text.CompareTo(REORDER)==0)

{

e.Effect = DragDropEffects.Move;

}

else

{

e.Effect = DragDropEffects.None;

}

}

Subsequently, OnDragOver ensures the continuing validity of the Drop operation as you move the mouse over the items in the control.

C#

Copy Code

protected override void OnDragOver(DragEventArgs e)

{

if(!this.AllowRowReorder)

{

e.Effect = DragDropEffects.None;

return;

}

if(!e.Data.GetDataPresent(DataFormats.Text))

{

e.Effect = DragDropEffects.None;

return;

}

Point cp = base.PointToClient(new Point(e.X, e.Y));

ListViewItem hoverItem = base.GetItemAt(cp.X, cp.Y);

if(hoverItem==null)

{

e.Effect = DragDropEffects.None;

return;

}

foreach(ListViewItem moveItem in base.SelectedItems)

{

if(moveItem.Index==hoverItem.Index)

{

e.Effect = DragDropEffects.None;

hoverItem.EnsureVisible();

return;

}

}

base.OnDragOver(e);

String text = (String)e.Data.GetData(REORDER.GetType());

if(text.CompareTo(REORDER)==0)

{

e.Effect = DragDropEffects.Move;

hoverItem.EnsureVisible();

}

else

{

e.Effect = DragDropEffects.None;

}

}

Finally OnDragDrop performs the actual Drop operation, this includes identifying the ListViewItems to be moved and the row index they will be moved to. Notice that I’m assuming that the ListViewItems to be moved are the control's currently selected items.

C#

Copy Code

protected override void OnDragDrop(DragEventArgs e)

{

base.OnDragDrop(e);

if(!this.AllowRowReorder)

{

return;

}

if(base.SelectedItems.Count==0)

{

return;

}

Point cp = base.PointToClient(new Point(e.X, e.Y));

ListViewItem dragToItem = base.GetItemAt(cp.X, cp.Y);

if(dragToItem==null)

{

return;

}

int dropIndex = dragToItem.Index;

if(dropIndex>base.SelectedItems[0].Index)

{

dropIndex++;

}

ArrayList insertItems =

new ArrayList(base.SelectedItems.Count);

foreach(ListViewItem item in base.SelectedItems)

{

insertItems.Add(item.Clone());

}

for(int i=insertItems.Count-1;i>=0;i--)

{

ListViewItem insertItem =

(ListViewItem)insertItems[i];

base.Items.Insert(dropIndex, insertItem);

}

foreach(ListViewItem removeItem in base.SelectedItems)

{

base.Items.Remove(removeItem);

}

}

Downloads

Source and demo projects have been created using #Develop.

Update

6th October 2003 - Fix for reordering problem when dragging multiple items.

10 November 2003 – Updated for automatic scrolling during a Drag operation (suggestion from JayBarry and Brian Pierson)

Android代码-DragListView DragListView DragListView can be used when you want to be able to re-order items in a list, grid or a board. It also supports horizontal swiping of items in a list. YouTube demo video Features Re-order items in a list, grid or board by dragging and dropping with nice animations. Add custom animations when the drag is starting and ending. Get a callback when a drag is started and ended with the position. Disable and enable drag and drop Swipe list items Download lib with gradle repositories { 立即下载

相关推荐

android-drag-and-drop-listview:Android 拖放 ListView

android-拖放列表视图 Android拖放ListView并执行短路

Android学习系列(11)--App列表之拖拽ListView(下)

接着上篇Android学习系列(10)--App列表之拖拽ListView(上)我们继续实现ListView的拖拽效果。 7.重写onTouchEvent()方法。 在这个方法中我们主要是处理拖动和放下。 拖动是选中项的影像随着手指滑动;放下是在拖动结束的时候交换数据。 方法的整体结构如下: /** * 触摸事件 ...

dianan1505的博客 198

[安卓开源]DragList(可拖拽移动的ListView)

不是固定的列表视图,可以随意的拖动!!!

PyCryptoBot模拟交易与回测系统:零风险测试你的交易策略

PyCryptoBot是一款功能强大的Python加密货币交易机器人,其内置的模拟交易与回测系统让用户能够在零风险环境下测试各种交易策略。通过模拟真实市场条件,你可以验证策略的有效性、优化参数设置,并在投入真实资金前充分了解策略的潜在表现。 ## 为什么选择模拟交易与回测? 加密货币市场波动剧烈,直接使用真实资金测试新策略可能带来不必要的风险。PyCryptoBot的模拟交易功能提供了安全的测

gitblog_00025的博客 628

SlideAndDragListView的使用

SlideAndDragListView支持ListView的Item的拖动排序、左右滑动事件,可自定义左右滑动显示文字、图标、位移,同时支持onItemClick、onItemLongClick等监听器,提供丰富的回调接口。在QQ5.0有类似功能

Android-AbsListView-Drag-And-Drop:具有拖放功能的复杂 ListView 和 GridView

Android-AbsListView-Drag-And-Drop 具有拖放功能的复杂 ListView 和 GridView。 拖拽项目到下拉菜单视图,回调下拉功能。 滑动选择 怎么看? 拖放和MultiChoice(官方和自定义) 滑动选择,例如 Google 相册 用法 多选 选择模式 选择模式.官方 它是AbsListView.setChoiceMode(CHOICE_MODE_MULTIPLE_MODAL) ,您还需要setMultiChoiceModeListener() 。 listView . setSelectionMode( SelectionMode . Official ); listView . setMultiChoiceModeListener(multiChoiceModeListener); 选择模式.自定义 这是一个自定义的多选模式,你应该

android基础知识39:App列表之拖拽ListView

研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨。       鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章。  一、准备。 1.需求问题       初步:实现列表的拖拽效果(可参考Android源码下packages/apps/Music中的播放列表TouchInterceptor.java

1446

可拖拽listview

从guihub上下载下来的demo改良版,该listview控件支持item拖拽,且item可横滑删除

终极指南:Shoelace网格布局与CSS Grid系统集成完整教程

Shoelace是一个现代化的Web组件库,现已演变为Web Awesome项目,它为开发者提供了丰富的UI组件和强大的CSS Grid系统集成能力。无论你是前端新手还是有经验的开发者,掌握Shoelace的网格布局技巧都能显著提升你的网页设计效率。 ## 🎯 为什么选择Shoelace的CSS Grid布局? Shoelace的设计哲学强调灵活性和可定制性,它的CSS Grid系统完美体现

gitblog_00349的博客 337

Android应用源码之DragList(可拖拽移动的ListView)-IT计算机-毕业设计.zip

Android应用源码开发Demo,主要用于毕业设计学习。

Android 中通过ViewDragHelper实现ListView的Item的侧拉划出效果

主要介绍了 Android 中通过ViewDragHelper实现ListView的Item的侧拉划出效果,需要的朋友可以参考下

Nginx Server Configs Magento配置:电商网站优化终极指南

想要让您的Magento电商网站拥有极致的性能和安全性吗?Nginx Server Configs提供了一套完整的Magento优化配置方案,能够显著提升网站加载速度、增强安全防护,为您的在线业务保驾护航。这套配置模板专门针对电商网站的高并发访问和安全需求进行了深度优化。 ## 🚀 为什么选择Nginx Server Configs? Nginx Server Configs是一个由社区驱动

gitblog_00031的博客 513

android RecyclerView实现条目Item拖拽排序与滑动删除

效果演示 需求和技术分析 RecyclerView Item拖拽排序::长按RecyclerView的Item或者触摸Item的某个按钮。 RecyclerView Item滑动删除:RecyclerView Item滑动删除:RecyclerView的Item滑动删除。 实现方案与技术 利用ItemTouchHelper绑定RecyclerView、ItemTouchHelper.Callback来实现UI更新,并且实现动态控制是否开启拖拽功能和滑动删除功能。 实现步骤 继承抽象类ItemTouchHelper,并在构造方法传入实现的ItemTouchHelper.Callba

推荐项目:DragNDropListView - 让你的Android列表动起来!

推荐项目:DragNDropListView - 让你的Android列表动起来! 在追求用户体验的今天,拖拽排序功能已成为众多应用提升交互性的重要手段。而对于Android开发者来说,DragNDropListView是一个不容忽视的强大工具,它使得在Android应用中实现直观的拖拽排序变得轻而易举。 项目介绍 DragNDropListView,正如其名,是原生Android ListVie...

gitblog_00333的博客 521

【Unity3D渲染】Unity3d实现Photoshop的各种图层混合模式

Adobe Photoshop图层混合原理、Unity ShaderLab图层混合实现

a310989583的专栏 2451

深入解析SOC芯片设计中DFT OCC与ATPG的协同优化策略

本文深入探讨了在复杂SOC芯片设计中,DFT(可测试性设计)的核心组件OCC(片上时钟控制器)与ATPG(自动测试向量生成)的协同优化策略。文章从基本原理出发,分析了OCC作为测试时钟管理者的关键作用,以及ATPG作为故障检测引擎的工作机制,并重点阐述了二者在测试架构选择、参数对齐、低功耗测试等场景下的协同方法与实战技巧,旨在实现更高的测试覆盖率与更低的测试成本。

efc123456的博客 1043

Taxonomy指标监控:10个性能指标追踪的终极指南

Taxonomy是一个基于Next.js 13新特性构建的开源应用,集成了新路由、服务器组件、Prisma、Radix UI和Tailwind CSS等现代技术栈。本指南将帮助你全面掌握Taxonomy应用的性能指标监控方法,通过10个关键指标追踪确保应用高效稳定运行。 [![Taxonomy应用架构概览](https://raw.gitcode.com/gh_mirrors/ta/taxono

gitblog_00065的博客 421

Pixhawk+OpenMV实战:如何用Apriltag实现无人机自动降落(附避坑指南)

本文详细介绍了如何利用Pixhawk飞控和OpenMV摄像头,通过Apriltag视觉标签实现无人机的精准降落系统。从硬件搭建、参数配置到视觉处理代码优化,提供了完整的实战指南和避坑建议,帮助开发者快速实现厘米级精度的自动降落功能。

weixin_27076351的博客 488

android listview item拖拽,自定义ListView实现拖拽ListItem项交换位置(附源码)

写在前面的话在上一篇实现了通过布局泵拿到不同布局为listitem布局,然后实现联系人的ListView,这一章要做的是拖拽ListView的Item项,本章原理是在上一篇博客基础之上的,上一篇博客:自定义Adapter并通过布局泵LayoutInflater抓取layout模板编辑每一个item实现效果图说明首先我们看到的上面这张图就是实现的效果图了。拖动之后数据项完成交换位置。功能剖析我们看...

weixin_33399354的博客 748

28-基于STM32单片机的便携式健康监测系统设计与实现(程序+原理图+元件清单)

本文详细介绍了基于STM32单片机的便携式健康监测系统设计与实现,该系统集成MAX30102光电传感器和MSP20压力传感器,实时监测心率、血氧和血压等关键生理指标。通过STM32F103C8T6主控芯片和HC-05蓝牙模块,实现数据采集、处理及无线传输,具有便携性高、操作简便等特点,适用于家庭健康监护和户外运动场景。

weixin_29191669的博客 434

SAP后台JOB保姆级教程:从SM36创建到SM37监控,手把手教你搞定ABAP程序定时任务

本文详细解析SAP后台JOB从创建到监控的全流程,重点介绍SM36创建和SM37监控的操作步骤与最佳实践。通过真实案例演示如何配置定时任务、处理常见故障及实施企业级管理策略,帮助ABAP开发者高效管理后台程序执行。

weixin_30838873的博客 442

android listview移动,SlideAndDragListView

SlideAndDragListView A ListView that you can slide(or swipe) the item, drag the item and drop it to another position.OverviewSlideAndDragListView (SDLV) is an extension of the Android...

weixin_33955307的博客 279

摇骰子、抽奖转盘酒桌游戏 人生重启模拟器小程序源码分享-开通流量主躺着赚钱

摇骰子、抽奖转盘酒桌游戏 人生重启模拟器小程序源码分享-开通流量主躺着赚钱

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 1658

android listview 移动,SlideAndDragListView

SlideAndDragListView A ListView that you can slide ( or swipe ) the items, drag and drop the items to other places.ChangeLog: CHINESE.mdOverviewSlideAndDragListView (SDLV) is an exten...

weixin_32647107的博客 245

ESP32-S3模组上兼容SCCB总线与I2C总线的解决方案(1)

ESP32-S3模组上兼容SCCB总线与I2C总线的解决方案(1)

phmatthaus的专栏 791

ANFIS实现数据非线性回归附Matlab代码

在本文中,我们将介绍如何使用Matlab实现基于MSE的ANFIS非线性回归,并提供相应的源代码。ANFIS是一种融合了模糊逻辑和神经网络的方法,它可以通过学习数据集中的模式来构建一个模糊推理系统。ANFIS的训练过程基于最小化MSE,以提高模型的拟合能力。通过上述代码示例,我们可以使用基于MSE的ANFIS模型对数据进行非线性回归,并获得相应的预测结果。在示例中,我们定义了一个包含两个样本的新输入数据集 X_new,然后使用训练好的ANFIS模型进行预测,并将结果存储在变量 Y_pred 中。

2301_79326588的博客 476
上一篇: 华为鸿蒙系统x2,华为多款机型开启鸿蒙尝鲜:微博已适配HarmonyOS小尾巴
下一篇: android 偶然乱码,post请求调用接口时,偶然会出现中文乱码,header中缺失Cookie:sid
weixin_39915171
博客等级 码龄9年 33粉丝 136原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值