对于
拖放
手势,大家并不生疏,这是在桌面端最稀松平时的操作,比方将文件拖入回收站。随着挪动设施的大屏趋势、可折叠设施的更加发欠缺,拖放操作在挪动平台里端也显得更加必要和风行!实现拖放手势: Android 平台现存的计划略为简单。基于此,
Jetpack
框架汇合里推出了新成员DragAndDrop
。_本文着重论述该框架的愿景和外围要点,次要内容译自 Android 开发者关系工程师
Paul
在Meduim
上的 Post
实质来说,拖放
手势(drag and drop)指的是用户通过点击抉择图片、文本或者其余数据元素,而后间接拖放到 App 的其余界面、甚至其余 App 的界面,接着这个数据就被纳入到新的界面内。这个手势通常体现为在触摸屏上的长按拖动
或者非触摸屏上的单击并用鼠标拖动
,最初在指标地位放下
。
来看一个 App 内典型的拖放成果:
只管 Android 始终长期反对拖放手势的实现(比方早在 Android 3.0
即退出的 DragEvent
API),但事实证明:想要残缺、顺畅地实现针对过程中的手势
、触摸事件
、权限
以及回调
的集成,往往比拟艰难和简单。
当初我想向大家举荐 Jetpack 的新成员 DragAnd Drop 框架”,目前处于 alpha
版本,其旨在辅助你更加简略地解决拖放 App 内的数据。
在 build.gradle 里引入依赖,即可应用。
implementation 'androidx.draganddrop:draganddrop:1.0.0-alpha02'
拖放手势的应用在大屏设施
上日益频繁,比方平板电脑和笔记本电脑,尤其是可折叠
设施。在这种类型的设施上进行分屏的操作比传统的智能手机多了高达 7 倍。他们的用户经常须要应用分屏
或多窗口
模式来解决多任务
的场景,而将数据在不同的 App 间拖放是再天然不过的体验和需要!
Android 平台原生曾经反对从输入框控件 EditText
拖动文本,但咱们强烈建议开发者实现用户从其余控件拖动数据的手势,反对的数据类型除了文本以外,还能包含图片、文件等任意类型。当然了,反向反对数据从其余 App 拖放进来也等同重要,并值得激励。
来看一个 App 之间拖放文本和图片的示例成果:
DragStartHelper
,联合 DropHelper
形成了整个框架最外围的 API,它们能够轻松实现手势反对、数据的回调、款式和像素级的 UI 对齐等实现环节。
DragStartHelper
作为 Jetpack 框架汇合 core
包下的工具类, DragStartHelper
负责监测拖动手势的开始机会。这些手势包含长按拖动、单击并用鼠标拖动等。
应用起来很简略,将须要监听的视图包装进来并开始监听。框架会在拖动手势触发的时候回调过去,之后进行一些简略的配置即可。
- 将须要传递的数据包装到
ClipData
中 - 新建用于展现拖动成果的图片实例
DragShadowBuilder
- 将数据和拖动成果外加一些 Flag 交由 View 的原生办法
startDragAndDrop()
进行后续的动作,包含成果的展现和数据的传递等
// Make a view draggable to share a file. DragStartHelper takes care of // intercepting drag gestures and attaching the listener. DragStartHelper(draggableView) { view, _ -> // Sets the appropriate MIME types automatically. val dragClipData = ClipData.newUri(contentResolver, "File", fileUri) // Set the visual look of the dragged object. // Can be extended and customized; we use the default here. val dragShadow = View.DragShadowBuilder(view) // Starts the drag. Note that the global flag allows for cross-app dragging. view.startDragAndDrop( dragClipData, dragShadow, null, // Optional extra local state information // Since this is a "content:" URI and not just plain text, we can use the // DRAG_FLAG_GLOBAL_URI_READ to allow other apps to read from our content // provider. Without it, other apps won't receive the drag events. DRAG_FLAG_GLOBAL or DRAG_FLAG_GLOBAL_URI_READ) ) }.attach()
DropHelper
另一个外围工具类 DropHelper
,则关怀拖动数据放下的机会和指标视图。
适配的代码简略来讲:
- 须要针对可拖放数据的试图调用
configureView
办法 - 其外部还须要设定关怀的数据类型即
Mime Type
- 指定一些其余可选参数实例
DropHelper.Options
,比方放下时高亮的色彩和视图范畴等 - 最初设置最重要的放下监听器
OnReceiveContentListener
,去从 ClipData 中获得数据执行上传、显示等解决,当然还包含不匹配的正告或视图揭示等
留神:构建 DropHelper.Options
实例的时候,记得调用 addInnerEditTexts()
,这样能够确保嵌套的 EditText 控件不会争夺视图焦点。
DropHelper.configureView( // Activity that will handle the drop this, // Target drop view to be highlighted outerDropTarget, // Supported MIME types arrayOf(MIMETYPE_TEXT_PLAIN, "image/*"), // Options for configuring drop targets DropHelper.Options.Builder() // To ensure proper drop target highlighting, all EditText elements in // the drop target view hierarchy must be included in a call to this // method. Otherwise, an EditText within the target, rather than the // target view itself, acquires focus during the drag and drop operation. .addInnerEditTexts(innerEditText) .build() ) { _, payload -> // Process the payload here, returning any content that should be delegated to // the platform. ... }
相干教程
Android根底系列教程:
Android根底课程U-小结_哔哩哔哩_bilibili
Android根底课程UI-布局_哔哩哔哩_bilibili
Android根底课程UI-控件_哔哩哔哩_bilibili
Android根底课程UI-动画_哔哩哔哩_bilibili
Android根底课程-activity的应用_哔哩哔哩_bilibili
Android根底课程-Fragment应用办法_哔哩哔哩_bilibili
Android根底课程-热修复/热更新技术原理_哔哩哔哩_bilibili
本文转自 https://juejin.cn/post/7043616310369976328,如有侵权,请分割删除。