当前位置:网站首页>The draganddrop framework, a new member of jetpack, greatly simplifies the development of drag and drop gestures!
The draganddrop framework, a new member of jetpack, greatly simplifies the development of drag and drop gestures!
2022-06-23 18:01:00 【User 9253515】
about
Drag and dropgesture , You are familiar with it , This is the most common operation on the desktop , For example, drag files into the recycle bin . With Large screen trend of mobile devices 、 Folding equipment is becoming more and more perfect , Drag and drop operation is becoming more and more necessary and popular in mobile platform ! Implement drag and drop gestures : Android The existing scheme of the platform is slightly complex . Based on this ,JetpackNew members have been introduced in the framework collectionDragAndDrop. _ This paper focuses on the vision and core points of the framework , The main content is translated from Android Developer Relationship EngineerPaulstayMeduimUpper Post In essence ,Drag and dropgesture (drag and drop) It means that the user selects the picture by clicking 、 Text or other data elements , Then drag and drop directly to App Other interfaces 、 even to the extent that other App The interface of , Then the data is incorporated into the new interface . This gesture is usually expressed as a gesture on the touch screenLong press dragOr not on the touch screenClick and drag with the mouse, Finally, at the target locationput down.
Come and have a look at App Classic drag and drop effect :
Even though Android It has long supported the implementation of drag and drop gestures ( As early as Android 3.0 That is to say, to join DragEvent API), But it turns out : Want to be complete 、 Smooth implementation of... In the process gesture 、 Touch event 、 jurisdiction as well as Callback Integration of , Often compare Difficult and complex .
Now I would like to recommend Jetpack New members of DragAnd Drop frame ", Currently in alpha edition , It is designed to help you deal with drag and drop more easily App The data in .
stay build.gradle We introduce dependency into the system , You can use .
implementation 'androidx.draganddrop:draganddrop:1.0.0-alpha02'
The use of drag and drop gestures in Large screen equipment More and more frequently , Like tablets and laptops , In especial folding equipment . Split screen operation on this type of device is up to... More than traditional smart phones 7 times . Their users often need to use Split screen or Many window Mode to handle multitasking Scene , And put the data in different App Drag and drop is a natural experience and demand !
Android The native platform already supports controls from input boxes EditText Drag text , But we strongly recommend that developers implement users From other controls Gesture of dragging data , Supported data types except text , Can also Including pictures 、 Any type of file . Yes, of course , Reverse support data From the other App Drag and drop in Equally important , And worthy of encouragement .
Come and have a look at App Between Example effects of dragging and dropping text and pictures :
DragStartHelper, combination DropHelper Constitutes the core of the whole framework API, They can be easily implemented Gesture support 、 Data callback 、 Style and pixel level UI alignment And so on .
DragStartHelper
As Jetpack Frame set core The tool class under the package , DragStartHelper be responsible for Monitor the start time of the drag gesture . These gestures include long press and drag 、 Click and drag with the mouse, etc .
It's simple to use , Wrap the view you need to listen in and start listening . The frame will call back when the drag gesture is triggered , After that, you can make some simple configuration .
- Wrap the data to be transferred into
ClipDatain - Create a new image instance to show the drag effect
DragShadowBuilder - Add some... To the data and drag effect Flag Leave it to View The original method of
startDragAndDrop()Carry out subsequent actions , Including effect display and data transmission
// 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
Another core tool class DropHelper, You care about dragging the data down opportunity and Target view .
The adapted code is simply :
- An attempt to call for draggable data is required
configureViewMethod - It also needs to set the concerned data type, i.e
Mime Type - Specify some other optional parameter instances
DropHelper.Options, For example, highlight when you put it down Color and View range etc. - Finally, set the most important thing to put down the listener
OnReceiveContentListener, To go from ClipData Get data from and upload it 、 Display and other processing , Of course, it also includes mismatched warnings or view reminders
Be careful : structure DropHelper.Options At instance time , Remember to call addInnerEditTexts(), This ensures that Nested EditText Control does not grab view focus .
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.
...
}Related courses
Android Basic series of tutorials :
Android basic course U- Summary _ Bili, Bili _bilibili
Android basic course UI- Layout _ Bili, Bili _bilibili
Android basic course UI- Control _ Bili, Bili _bilibili
Android basic course UI- Animation _ Bili, Bili _bilibili
Android basic course -activity Use _ Bili, Bili _bilibili
Android basic course -Fragment Usage method _ Bili, Bili _bilibili
In this paper, from https://juejin.cn/post/7043616310369976328, If there is any infringement , Please contact to delete .
边栏推荐
- Tencent Qianfan scene connector: worry and effort saving automatic SMS sending
- Skills that all applet developers should know: applying applet components
- Hands on data analysis unit 2 section 4 data visualization
- Listen attentively and give back sincerely! Pay tribute to the best product people!
- 12. Manage network environment
- Redis ubuntu18.04.6 intranet deployment
- Nanny level teaching! Take you to play with time complexity and space complexity!
- How to use R language to draw scatter diagram
- README
- Interpretation of eventbus source code
猜你喜欢

Date selection of hotel check-in time and check-out time

全局组织结构控制之抢滩登陆
![QT layout manager [qvboxlayout, qhboxlayout, qgridlayout]](/img/62/a0425a325f123ba91a7a69cf854d2c.png)
QT layout manager [qvboxlayout, qhboxlayout, qgridlayout]

Meituan Sanmian: how do you understand the principle of redis master-slave replication?

论文阅读 (52):Self-Training Multi-Sequence Learning with Transformer for Weakly Supervised Video Anomaly

JSON - learning notes (message converter, etc.)

论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks

论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...

MySQL transaction and its characteristics and locking mechanism

Self supervised learning (SSL)
随机推荐
Ner's past, present and future Overview - Future
Hapoxy cluster service setup
Cryptography involved in IOT device end
Establishment and use of SSL VPN (OpenVPN)
手机开户一般哪个证券公司好?在线开户安全么?
Also using copy and paste to create test data, try the data assistant!
Goframe framework: basic auth Middleware
How to create a three elimination game
How to quickly obtain and analyze the housing price in your city?
Kotlin invoke convention makes kotlin code more concise
Postgresql_ Optimize SQL based on execution plan
Drawing black technology - easy to build a "real twin" 2D scene
Baidu AI Cloud product upgrade Observatory in May
What is the problem with TS File Error 404 when easynvr plays HLS protocol?
Answer 02: why can Smith circle "allow left string and right parallel"?
History of storage technology: from tape to hardware liquefaction
How to use JSON data format
对抗攻击与防御 (1):图像领域的对抗样本生成
Installation, configuration, désinstallation de MySQL
Performance test bottleneck tuning in 10 minutes! If you want to enter a large factory, you must know