Android Popupwindow Drag

Source: Internet
Author: User

Version: 1.0

Date: 2014.4.29

Copyright: © kince reproduced in the source


About the view of the drag people should be more familiar with, such as a control iamgeview drag, or a view views drag, implementation is also very easy to inherit the Ontouchlistener interface, and then rewrite the Ontouch method, in the touch screen event processing can be. But Popupwindow how to implement the drag, we all know that it is not the same as the normal view, because it is not inherited from the view class, but its implementation is closely related to the view, because we all know that the display of the Android view is handled by view, So it must be inseparable from it. From the realization of the Popupwindow can be seen,

Import Com.android.internal.r;import Android.content.context;import Android.content.res.resources;import Android.content.res.typedarray;import Android.graphics.pixelformat;import Android.graphics.rect;import Android.graphics.drawable.drawable;import Android.graphics.drawable.statelistdrawable;import Android.os.Build; Import Android.os.ibinder;import Android.util.attributeset;import Android.view.gravity;import Android.view.keyevent;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.view.viewgroup;import Android.view.viewtreeobserver;import Android.view.viewtreeobserver.onscrollchangedlistener;import Android.view.windowmanager;import Java.lang.ref.WeakReference;
The above is its guide package situation, is basically not related to the view, is related to the drawing. So dragging this piece about Popupwindow is also linked to view. First look at its API, see if there is any method related to the view move, change, and sure enough at the end there are several update () methods, as follows:


The update () method is used for updating the location and size of the Popupwindow, so the problem is solved. Look at the code:

Package Com.example.drag_and_drop_movablepopupwindow;import Android.support.v7.app.actionbaractivity;import Android.graphics.color;import Android.os.bundle;import Android.view.gravity;import Android.view.LayoutInflater; Import Android.view.menu;import android.view.menuitem;import android.view.motionevent;import Android.view.View; Import Android.view.view.ontouchlistener;import Android.view.viewgroup.layoutparams;import Android.widget.Button; Import Android.widget.popupwindow;import Android.widget.textview;public class Mainactivity extends ActionBarActivity {private Button btnopenpopup;private int mcurrentx;private int mcurrenty;private Popupwindow mpopup; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); btnopenpopup = (Button) Findviewbyid (r.id.openpopup); Btnopenpopup.setonclicklistener (new Button.onclicklistener () {@Overridepublic void OnClick (View arg0) {creatpopubwindow_1 ();}});} /** * 1 */private void Creatpopubwindow_1 () {Layoutinflater layoutinflater = (layoutinflater) getbasecontext (). Getsystemservice (LAYOUT_ Inflater_service); View Popupview = layoutinflater.inflate (r.layout.popup, NULL); final Popupwindow Popupwindow = new Popupwindow (popupView , 200, 200); Button Btndismiss = (button) Popupview.findviewbyid (R.id.dismiss); Btndismiss.setonclicklistener (new Button.onclicklistener () {@Overridepublic void OnClick (View v) {Popupwindow.dismiss ();}}); Popupwindow.showasdropdown (Btnopenpopup, ());p Opupview.setontouchlistener (new Ontouchlistener () {int OrgX, OrgY; int OffsetX, OffsetY; @Overridepublic boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) {case Motione Vent. ACTION_DOWN:ORGX = (int) event.getx (); orgy = (int) event.gety (); Break;case MotionEvent.ACTION_MOVE:offsetX = (int) event . GETRAWX ()-orgx;offsety = (int) Event.getrawy ()-Orgy;popupwindow.update (OffsetX, OffsetY,-1,-1, true); return true;}});}}

Effect


First, the touch event is set on Popupwindow and then evaluated in the callback method, and the update () method is called to update its position if the finger drags Popupwindow. Some students may not understand the parameter 1 is what meaning, in the above API, stated is the width and height, here how to become-1, look at the Popupwindow source code to understand.

 /** * <p>updates The position and the dimension of the popup window.  Width and height can set to-1 to update location only. Calling this function * also updates the windows with the current popup state as * described for {@link #update ()}.  </p> * @param x the new x location * @param y the new y location * @param width The new width, can Be-1 to ignore * @param height The new height, can be-1 to ignore * @param force reposition the window even if  The specified position * already seems to correspond to the layoutparams */public void update (int            x, int y, int width, int height, Boolean force) {if (width! =-1) {mlastwidth = width;        SetWidth (width);            } if (Height! =-1) {mlastheight = height;        SetHeight (height);        } if (!isshowing () | | | mcontentview = = NULL) {return; } Windowmanager.layoutparams p = (windOwmanager.layoutparams) Mpopupview.getlayoutparams ();        Boolean update = Force; Final int finalwidth = Mwidthmode < 0?        Mwidthmode:mlastwidth;            if (Width! =-1 && p.width! = finalwidth) {p.width = Mlastwidth = Finalwidth;        Update = TRUE; } final int finalheight = Mheightmode < 0?        Mheightmode:mlastheight;            if (height! =-1 && p.height! = finalheight) {p.height = Mlastheight = Finalheight;        Update = TRUE;            } if (p.x! = x) {p.x = x;        Update = TRUE;            } if (p.y! = y) {p.y = y;        Update = TRUE;        } final int newanim = Computeanimationresource ();            if (Newanim! = p.windowanimations) {p.windowanimations = Newanim;        Update = TRUE;        } final int newflags = Computeflags (p.flags);            if (newflags! = p.flags) {p.flags = Newflags;   Update = TRUE;     } if (update) {setlayoutdirectionfromanchor ();        Mwindowmanager.updateviewlayout (Mpopupview, p); }    }
The first two if judgments have been made very clear, if the parameter is-1, it does not change the size of the Popupwindow, because we just move the position, so it is written. The last thing about Popupwindow is how to implement it, it can be seen that is called WindowManager updateviewlayout () method, this method is not implemented in WindowManager, It is the method inside the Viewmanager interface, WindowManager inherits the Viewmanager. When it comes to Viewmanager, the methods defined in it are very common, see the code:

/** Interface to let you add and remove a child views to an Activity. To get a instance * of this class, call {@link Android.content.context#getsystemservice (java.lang.String) Context.getsys  Temservice ()}. */public interface viewmanager{/** * Assign the passed layoutparams to the passed view and add the view to the win     Dow. * <p>throws {@link android.view.WindowManager.BadTokenException} for certain programming * errors, such as AddIn     G A second view to a window without removing the first view. * <p>throws {@link android.view.WindowManager.InvalidDisplayException} if the window is on a * secondary {@link     Display} and the specified display can ' t be found * (see {@link android.app.Presentation}).     * @param view the view to is added to this window.     * @param params the layoutparams to assign to view.    */public void AddView (view view, viewgroup.layoutparams params); public void Updateviewlayout (view view, viewgroup.layoutparams params);    public void Removeview (view view); 
It should be understood that we often use the AddView, Removeview method is defined here, then who to achieve it? is the layout control, such as LinearLayout, Relativelayout, and so on, so the Updateviewlayout () method We just used is also defined in the layout of the XML layouts file.





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.