Android: Drag In Addition To Driblet A Stance Onto Merely About Other View


Understanding the Drag as well as the Drop

In enabling a user to perform a driblet as well as drag activity yous convey a persuasion that is being dragged as well as a persuasion that is being dragged to. The listener that volition activate the drag, e.g. a long press, is attached to the persuasion that volition survive dragged. While the persuasion that is beingness dragged to has the drag listener attached to it. Influenza A virus subtype H5N1 unmarried persuasion tin survive dragged onto many dissimilar views if those views each convey drag listeners laid as well as prepare to reply positively. Likewise multiple views tin survive dragged onto the same persuasion that is beingness dragged to.

View Being Dragged To

Starting alongside the persuasion beingness dragged to, which is an essential if yous are going to survive able to driblet your draggable persuasion anywhere, nosotros convey the next code. (See below for farther explanation.)
// Influenza A virus subtype H5N1 persuasion is identified from the layout file inside which other views tin survive dragged (mainView) RelativeLayout mainView = findViewById(R.id.mainView);  mainView.setOnDragListener(new View.OnDragListener() {     @Override     world boolean onDrag(View view, DragEvent dragEvent) {                  switch (dragEvent.getAction()) {              instance ACTION_DRAG_STARTED:                 render true;             instance ACTION_DRAG_ENTERED:                 render true;             instance ACTION_DRAG_LOCATION:                 render true;             instance ACTION_DRAG_EXITED:                 render true;             instance ACTION_DROP:                 float newX = dragEvent.getX();                 float newY = dragEvent.getY();                   View dragView = (View) dragEvent.getLocalState();                      dragView.setX(newX - dragView.getWidth()/2);                 dragView.setY(newY - dragView.getHeight()/2);                 render true;             instance ACTION_DRAG_ENDED:                 render true;             default:                 render false;          }      }   }); 

View Being Dragged

Next nosotros convey the persuasion beingness dragged, which starts the procedure of dragging alongside the startDrag method. Influenza A virus subtype H5N1 convenient means to grade the wish to get down dragging is inside a long click listener.
// Influenza A virus subtype H5N1 persuasion is created that volition survive dragged.    ImageView newImg = novel ImageView(this);         newImg.setMinimumWidth(100);         newImg.setMinimumHeight(100);         newImg.setBackgroundColor(getResources().getColor(R.color.colorPrimary));         mainView.addView(newImg);          imageView.setOnLongClickListener(new View.OnLongClickListener() {      // Defines the ane method for the interface, which is called when the View is long-clicked     world boolean onLongClick(View v) {          // Instantiates the drag shadow builder.         View.DragShadowBuilder myShadow = novel View.DragShadowBuilder(v);           // Starts the drag         v.startDrag(null,  // the information to survive dragged                 myShadow,  // the drag shadow builder                 v,      // no require to role local information                 0          // flags (not currently used, laid to 0)         );         render true;     }   });
This is all in that location is to drag as well as drop, where the aim is to movement a persuasion of about sort to about other position. To experiment this code tin safely survive placed inside the opening onCreate method of the MainActivity.

Further Detail

The OnDragListener has a unmarried method contained inside its closure: onDrag (View v, DragEvent event).

The onDrag method receives a DragEvent instance, which identifies ane of half dozen actions

ACTION_DRAG_STARTED
ACTION_DRAG_ENTERED
ACTION_DRAG_LOCATION
ACTION_DROP
ACTION_DRAG_EXITED
ACTION_DRAG_ENDED

through a getAction() call. It is thus your responsibleness to response truthful or imitation every bit to whether the activity has been handled. Returning truthful identifies that the activity has been handled successfully, returning imitation indicates failure.

The persuasion passed to the onDrag method is the persuasion being dragged to, piece the final result holds all the DragEvent information.

We don’t convey a straight reference to the persuasion beingness dragged as well as yous powerfulness think that yous require to somehow code this information into the ClipData (which tin survive transported alongside the drag - meet Android Developer Docs on Drag as well as Drop) simply thankfully this isn’t the instance because this information tin survive retrieved past times casting the local nation to a View.

This casting of the local nation is a somewhat mysterious human activity because all nosotros know close the local nation from the docs is that:
The object is intended to render local information close the drag as well as driblet operation. For example, it tin dot whether the drag as well as driblet functioning is a re-create or a move.
The local nation is available alone to views inward the activity which has started the drag operation. In all other activities this method volition render null
This method returns valid information for all final result actions.
And thus yous would aspect to function through an array of objects, or such similar to larn inward at the reference to the persuasion beingness dragged. But no, a uncomplicated cast is all that is required, as well as presumably re-create as well as movement information is extracted inward a similar way, simply I convey non nevertheless investigated this.

Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?