Launcher drag process summary "Android 2.3 2.2 』

Source: Internet
Author: User

Q: Nima Android 4.1 jelly bean has been released. You still have bb2.3, right?

A: Sorry, It is a feature of diaosi.

Q: What is the purpose?

A: It is very irresponsible to say that the launcher drag process has not changed since 2.2.

Q: What is the basic concept? There are still changes.

A: "Okay, you win ".

Bytes -------------------------------------------------------------------------------------------------------------I am a style line--------

Back to the question: If launcher is used, the drag-and-drop event processing and response must be clear:

First, let's take a sequence chart of launcher drag and drop to get a rough grasp of the entire process (it doesn't matter if you don't understand it. Skip to the next step and look back at it again to make it so easy ):


The following steps are detailed:

1. Event generation

Drag-and-drop events are generated based on the user's long-pressed operation in two cases:

  • The sub-view (application icon, folder, widget) on the desktop (workspace view) is responded to by launcher. java.

Public Boolean onlongclick (view v) {...... else {If (! (Cellinfo. Cell instanceof folder) {// call mworkspace. startdrag (cellinfo) ;}}return true when you press a component ;}

Jump to F2:

   void startDrag(CellLayout.CellInfo cellInfo)    {        View child = cellInfo.cell;        // Make sure the drag was started by a long press as opposed to a long click.        // Note that Search takes focus when clicked rather than entering touch mode      ..........        mDragger.startDrag(child, this, child.getTag(), DragController.DRAG_ACTION_MOVE);        invalidate();        clearVacantCache();    }

  • The sub-view (application icon) event response on the application main menu (allappgridview view) is allappgridview. Java

Public Boolean onitemlongclick (adapterview <?> Parent, view, int position, long ID) {If (! View. isintouchmode () {return false;} applicationinfo APP = (applicationinfo) parent. getitematposition (position); APP = new applicationinfo (APP); // call mdragger when you long press the application icon on the main menu of the application. startdrag (view, this, app, dragcontroller. drag_action_copy );}

The event generation in this step is finally located in the mdragger. startdrag method, just move it down.

2. What is mdragger?

Mdragger is a dragcontroller instance. We can see in F3 that dragcontroller is actually an interface with several well-known abstract methods.

F4: You can see that the only subclass of this interface is draglayer. java. You have to go in and find out. Nima actually inherits framelayout. There is something flashing in your mind, Soga. It turns out to be like this:

<com.android.launcher2.DragLayer    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"    android:id="@+id/drag_layer"    android:layout_width="match_parent"    android:layout_height="match_parent">    <!-- Keep these behind the workspace so that they are not visible when         we go into AllApps -->    <include

...........

The above code is the main layout file of launcher. As the rootview of the entire launcher, draglayer is responsible for unified distribution and handling of all drag and drop events. Because all the components that can be viewed by launcher are deployed on it.

Why is it framelayout.

When you press a launcher component for a long time, draglayer. startdrag will trigger a throw to inform the relevant components to prepare for the drag event and quietly hide the original view of the drag icon.

3. Since the framelayout view has been followed, it is more intuitive to follow the touch screen timing processing.

 public boolean onInterceptTouchEvent(MotionEvent ev)     {      ..........            case MotionEvent.ACTION_UP:                if (mShouldDrop && drop(x, y))                 {                    mShouldDrop = false;                }                endDrag();                break;        }        return mDragging;    }

Onintercepttouchevent is not used here as the "interrupt" of the touch screen event, but enddrag is called during up to display the hidden view when appropriate.

The boss is busy, so naturally the younger brother is better than the boss. See how ontouchevent handles these drag events in an orderly manner.

Public Boolean ontouchevent (motionevent eV) {... switch (Action) {Case motionevent. action_down: // initialize the mscrollstate state. Mscrollstate = scroll_outside_zone;} break; Case motionevent. action_move: // the position of the current icon is constantly updated. Rect. union (left-1, top-1, left + width + 1, top + height + 1); Final int [] coordinates = mdropcoordinates; // call finddroptarget to find the destination. For example, in the deletezone region, the related event response is triggered. Droptarget = finddroptarget (INT) x, (INT) y, coordinates );........ droptarget. ondragover (mdragsource, coordinates [0], coordinates [1], mlastdroptarget. ondragexit (mdragsource, coordinates [0], coordinates [1], droptarget. ondragenter (mdragsource, coordinates [0], coordinates [1], mlastdroptarget. ondragexit (mdragsource, coordinates [0], coordinates [1],... if (! Indragregion & x <scroll_zone) {If (mscrollstate = scroll_outside_zone) {mscrollstate = scroll_waiting_in_zone; mscrollrunnable. setdirection (scroll_left); // drag the icon to the left. Postdelayed (mscrollrunnable, scroll_delay) ;}} else if (! Indragregion & x> getwidth ()-scroll_zone) {If (mscrollstate = scroll_outside_zone) {mscrollstate = rollback; mscrollrunnable. setdirection (scroll_right); // drag the icon to the right to change the screen. Postdelayed (mscrollrunnable, scroll_delay );}}....... case motionevent. action_up: removecallbacks (mscrollrunnable); If (mshoulddrop) {// after the autumn, the general ledger is calculated. Whether the target view accepts the source view is determined by dropping (x, y. Drop (x, y); mshoulddrop = false;} enddrag (); break; Case motionevent. action_cancel: enddrag ();} return true ;}

In the action_move event, pay special attention to the implementation of the finddroptarget method. This event will continuously call the several abstract methods of droptarget, as long as it implements the view (userfolder, deletezone, workspace) of this interface) the corresponding overload method will be called according to specific conditions to make responses such as folder opening and closing and deletezone discoloration.

In the action_up event, we are very familiar with F3 and entered the drop method (it is too important, it determines the final result of the drag event, whether the target view accepts it or rejects the award ). But the code is actually very short:

Private Boolean drop (float X, float y) {invalidate (); Final int [] coordinates = mdropcoordinates; // If the finddroptarget above is a foothold, it is ready to be entrusted for life. Droptarget = finddroptarget (INT) x, (INT) y, coordinates); If (droptarget! = NULL) {If (shouldaccept = false & droptarget. tostring () = "actionbutton") {return false;} droptarget. ondragexit (mdragsource, coordinates [0], coordinates [1], (INT) mtouchoffsetx, (INT) mtouchoffsety, mdraginfo); // If acceptdrop returns true, this means that the men's parents have been given their approval and finally find the attached view. If (droptarget. acceptdrop (mdragsource, coordinates [0], coordinates [1], (INT) mtouchoffsetx, (INT) mtouchoffsety, mdraginfo) {droptarget. ondrop (mdragsource, coordinates [0], coordinates [1], (INT) mtouchoffsetx, (INT) mtouchoffsety, mdraginfo); mdragsource. ondropcompleted (View) droptarget, true); Return true;} else {mdragsource. ondropcompleted (View) droptarget, false); Return true ;}} return false ;}


As a matter of fact, I have already read this item last year. I am very lazy. Today I have finally finished it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.