Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add drag and drop effect to View in Android

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to add drag-and-drop effect for View in Android". In daily operation, I believe many people have doubts about how to add drag-and-drop effect to View in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to add drag-and-drop effect for View in Android". Next, please follow the editor to study!

1. Introduction

In development, drag and drop is a common gesture operation, which can make the interaction of applications more convenient and friendly.

two。 The main methods and classes introduce 2.1 startDragAndDrop () and startDrag ()

To implement drag and drop of View, you need to call the startDragAndDrop () or startDrag () method of View, where the startDragAndDrop () method requires API version 24 or above. After calling the method, View can drag. The parameters passed by this method are as follows:

/ / data: data to be passed by drag-and-drop operation / / shadowBuilder: drag-and-drop shadow / / myLocalState: an object containing data related to drag-and-drop operation / / flags: flag bit public final boolean startDragAndDrop (ClipData data, DragShadowBuilder shadowBuilder,Object myLocalState, int flags) 2.2 setOnDragListener () that controls drag-and-drop operation

The View that receives the drag-and-drop event is temporarily called the target View. The target View calls setOnDragListener () and implements the method onDrag () in it to receive the callback of the drag-and-drop event.

2.3 View.DragShadowBuilder

In the drag and drop operation, you need to display the picture you are dragging. The View.DragShadowBuilder class provides a construction method that can be passed into View. The View is the View that is dragged and dropped. The drag image created by DragShadowBuilder is called drag shadow, which will be passed into the startDragAndDrop () or startDrag () method as a parameter. If necessary, you can inherit the View.DragShadowBuilder class to achieve the custom effect.

2.4 DragEvent

This class mainly defines the types of drag-and-drop events. Different event types can be obtained through event.getAction (), mainly as follows:

/ / DragEvent.ACTION_DRAG_STARTED: indicates that the drag has started / / DragEvent.ACTION_DRAG_ENTERED: indicates that the drag shadow has entered the target View//DragEvent.ACTION_DRAG_LOCATION: when the drag shadow moves within the boundary of the target View, it will respond to this event multiple times / / DragEvent.ACTION_DRAG_EXITED: indicates that the drag shadow has left the boundary of the target View / / DragEvent.ACTION_DROP: indicates that the drag shadow has been released / / DragEvent.ACTION _ DRAG_ENDED: indicates that the drag and drop operation is about to end Here, you need to determine whether the drag-and-drop operation was successful by calling the return value of event.getResult (). Demonstrate dragging and dropping a picture into the box

Now demonstrate dragging and dropping a picture into a box to illustrate the general flow of the drag-and-drop operation, where the box is a LinearLayout and sets a box background for it.

3.1 simple layout

The layout is as follows:

3.2 manipulate dragged and dropped pictures

To achieve the effect of dragging a picture with a long press, first call setOnLongClickListener () to set the long press event callback, then build ClipData and drag shadow, and then call the startDragAndDrop () or startDrag () method to achieve drag. The code is as follows:

Iv_drag.setOnLongClickListener (new View.OnLongClickListener () {@ Override public boolean onLongClick (View v) {CharSequence charSequence = (CharSequence) iv_drag.getTag (); ClipData.Item item = new ClipData.Item (charSequence); ClipData clipData = new ClipData (charSequence, new String [] {ClipDescription.MIMETYPE_TEXT_PLAIN}, item); View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder (iv_drag) If (Build.VERSION.SDK_INT > = Build.VERSION_CODES.N) {iv_drag.startDragAndDrop (clipData, shadowBuilder, null, 0);} else {iv_drag.startDrag (clipData, shadowBuilder, null, 0);} return true;}})

To receive pictures, the target View needs to call setOnDragListener () to accept the callback of drag-and-drop events, use event.getAction () to get different drag-and-drop event types, and then perform corresponding actions according to the event types. Examples are as follows:

Ll_accept.setOnDragListener (new View.OnDragListener () {@ Override public boolean onDrag (View v, DragEvent event) {switch (event.getAction ()) {case DragEvent.ACTION_DRAG_STARTED: iv_drag.setVisibility (View.GONE); if (event.getClipDescription (). HasMimeType (ClipDescription.MIMETYPE_TEXT_PLAIN)) {return true } return false; case DragEvent.ACTION_DRAG_ENTERED: isChangePos = true; ll_accept.setBackgroundResource (R.drawable.green_bac); return true; case DragEvent.ACTION_DRAG_LOCATION: X = event.getX (); y = event.getY () Return true; case DragEvent.ACTION_DRAG_EXITED: isChangePos = false; ll_accept.setBackgroundResource (R.drawable.black_bac); return true; case DragEvent.ACTION_DROP: return true Case DragEvent.ACTION_DRAG_ENDED: ll_accept.setBackgroundResource (R.drawable.black_bac); if (isChangePos & & event.getResult ()) {int left = ll_accept.getLeft (); int top = ll_accept.getTop (); x = x + left-(iv_drag.getWidth () / 2) Y = y + top-(iv_drag.getHeight () / 2); iv_drag.setX (x); iv_drag.setY (y);} iv_drag.setVisibility (View.VISIBLE); return true;} return false;}}) At this point, the study on "how to add drag-and-drop effects to View in Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report