Look at the effect chart first.
Main ideas:
1. Monitor the Touch event
2, with WindowManager to add drag pictures
3. Exchange list data with Collections.swap ()
Custom code:
public class Draggridveiw extends GridView {private final int press_time = 1000;//long time private int mdownx;//touch x sit Mark Private int mdowny;//The y-coordinate of the contact when private int mmovex;//move when the x coordinates private int mmovey;//when moving the y-coordinate private int moffset2top; Draggridview the offset from the top of the screen private int moffset2left;//draggridview The offset from the left side of the screen private int mpointtoitemtop;// Touch Point distance itemview the top margin private int mpointtoitemleft;//Touch point distance itemview the left margin private int mstatusheight;//status bar height private Boolea n isdraging;//Whether you are dragging private Bitmap Mbitmap;//itemview's picture private int mtouchpostiion;//Touch location Private View mtouchite mview;//Touch Itemview Private Vibrator mvibrator;//Vibrator Private ImageView mdragimageview;//Drag view private Windowmanag Er mwindowmanager;//window manager private windowmanager.layoutparams mwindowlayoutparams;//window manager Layout Private Onchanagelistener
onchanagelistener;//Exchange Event Listener private Handler Mhandler = new Handler ();
Public Draggridveiw {This (context, NULL); } public DragGridveiw (context, AttributeSet attrs) {This (context, attrs, 0);
Public Draggridveiw (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);
Mstatusheight = getstatusheight (context);
Mvibrator = (Vibrator) context.getsystemservice (Context.vibrator_service);
Mwindowmanager = (WindowManager) context.getsystemservice (Context.window_service); @Override public boolean dispatchtouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent . Action_down://Use handler delay Dragresponsems execution mlongclickrunnable mhandler.postdelayed (mlongclickrunnable, PRES
S_time);
MDOWNX = (int) ev.getx ();
Mdowny = (int) ev.gety ();
To obtain the position mtouchpostiion = Pointtoposition (Mdownx, Mdowny) of the item clicked according to the x,y coordinates;
if (mtouchpostiion = = adapterview.invalid_position) {return super.dispatchtouchevent (EV);
///According to position to get the item's view Mtouchitemview = Getchildat (Mtouchpostiion-getfirstvisibleposition ());
Below these several distances everybody may refer to my blog above diagram to understand under Mpointtoitemtop = Mdowny-mtouchitemview.gettop ();
Mpointtoitemleft = Mdownx-mtouchitemview.getleft ();
Moffset2top = (int) (Ev.getrawy ()-mdowny);
Moffset2left = (int) (EV.GETRAWX ()-MDOWNX);
Open Mdragitemview Drawing Cache mtouchitemview.setdrawingcacheenabled (TRUE);
Gets the Bitmap object mbitmap = Bitmap.createbitmap (Mtouchitemview.getdrawingcache ()) of the Mdragitemview in the cache;
This step is critical, freeing up the drawing cache to avoid duplicate mirror Mtouchitemview.destroydrawingcache ();
Break
Case MotionEvent.ACTION_MOVE:int MoveX = (int) ev.getx ();
int movey = (int) ev.gety ();
The drag point exceeds the GridView area and the drag event is canceled if (Ev.gety () > getheight () | | ev.gety () < 0) {Onstopdrag ();
//If we move above the pressed item, remove mrunnable if (!istouchinitem (Mtouchitemview, MoveX, Movey) if it is over the item's bounds) { MhanDler.removecallbacks (mlongclickrunnable);
} break;
Case MotionEvent.ACTION_UP:mHandler.removeCallbacks (mlongclickrunnable);
Break
return super.dispatchtouchevent (EV);
@Override public boolean ontouchevent (motionevent ev) {if (isdraging && mdragimageview!= null) {
Switch (ev.getaction ()) {Case MotionEvent.ACTION_MOVE:mMoveX = (int) ev.getx ();
Mmovey = (int) ev.gety ();
Drag the item Ondragitem (Mmovex, Mmovey);
Break
Case MotionEvent.ACTION_UP:onStopDrag ();
Break
return true;
return super.ontouchevent (EV); }//Handle long thread by event private Runnable mlongclickrunnable = new Runnable () {@Override public void run () {IsD Raging = true; Settings can be dragged mvibrator.vibrate (50); Shake the Mtouchitemview.setvisibility (view.invisible);//Hide the Itemview////According to the point we pressed the itemview mirror image Createdragview (MBitmap, Mdownx, Mdowny);
}
}; Add drag view private void Createdragview (Bitmap Bitmap, int downx, int downY) {mwindowlayoutparams = new windowmanage
R.layoutparams (); Mwindowlayoutparams.format = pixelformat.translucent; Other places outside the picture transparent mwindowlayoutparams.gravity = Gravity.top |
Gravity.left;
Mwindowlayoutparams.x = Downx-mpointtoitemtop + moffset2left;
Mwindowlayoutparams.y = Downy-mpointtoitemtop + moffset2top-mstatusheight; Mwindowlayoutparams.alpha = 0.6f;
Transparency mwindowlayoutparams.width = WindowManager.LayoutParams.WRAP_CONTENT;
Mwindowlayoutparams.height = WindowManager.LayoutParams.WRAP_CONTENT; Mwindowlayoutparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
Mdragimageview = new ImageView (GetContext ());
Mdragimageview.setimagebitmap (bitmap);
Mwindowmanager.addview (Mdragimageview, mwindowlayoutparams); private void Removedragview () {if (MDRagimageview!= null) {Mwindowmanager.removeview (Mdragimageview);
Mdragimageview = null; }//Whether to click on the GridView Item private Boolean Istouchinitem (View dragview, int x, int y) {int leftoffset = Dragvi
Ew.getleft ();
int topoffset = Dragview.gettop ();
if (x < LeftOffset | | | | x > LeftOffset + dragview.getwidth ()) {return false;
} if (Y < Topoffset | | y > topoffset + dragview.getheight ()) {return false;
return true; }//drag event handling private void Ondragitem (int moveX, int movey) {mwindowlayoutparams.x = Movex-mpointtoitemleft + mOf
Fset2left;
Mwindowlayoutparams.y = Movey-mpointtoitemtop + moffset2top-mstatusheight; Mwindowmanager.updateviewlayout (Mdragimageview, mwindowlayoutparams); Update dragview location Onswapitem (MoveX, Movey);//item Interchange}//Exchange item, and control display and hidden effects between item private void Onswapitem (int MoveX, int movey) {//Get the position int tempposition = Pointtoposition of the item that our fingers move to (MoVex, Movey); If tempposition changes and tempposition is not equal to-1, then swap if (tempposition!= mtouchpostiion && tempposition!= w.invalid_position) {Getchildat (Tempposition-getfirstvisibleposition ()). Setvisibility (View.INVISIBLE);// Drag to the new item, the new item hides the Getchildat (Mtouchpostiion-getfirstvisibleposition ()). Setvisibility (view.visible);
The previous item shows the IF (Onchanagelistener!= null) {Onchanagelistener.onchange (mtouchpostiion, tempposition);
} mtouchpostiion = Tempposition;
}//Stop dragging us to display the previously hidden item and remove Dragview from private void Onstopdrag () {isdraging = false;
Getchildat (Mtouchpostiion-getfirstvisibleposition ()). Setvisibility (view.visible);
Removedragview (); //item Exchange Event Listener public void Setonchangelistener (Onchanagelistener onchanagelistener) {This.onchanagelistener = on
Chanagelistener;
//Get status bar height private int getstatusheight (context context) {int statusheight = 0; Rect localrect = new RECT ();
(activity). GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (Localrect);
Statusheight = Localrect.top;
if (0 = = statusheight) {class<?> localclass;
try {localclass = Class.forName ("Com.android.internal.r$dimen");
Object localobject = Localclass.newinstance ();
int i5 = integer.parseint (Localclass.getfield ("Status_bar_height"). Get (Localobject). toString ());
Statusheight = Context.getresources (). Getdimensionpixelsize (i5);
catch (Exception e) {e.printstacktrace ();
} return statusheight; //When the item is exchanged for the callback method, we only need to implement the data exchange in this method to public interface Onchanagelistener {The public void OnChange (int from, int
To);
}
}
using methods:
Appendix:
LOG.V ("-->getwidth", String.valueof (GetWidth ());//dragview width log.v ("--> GetHeight ", String.valueof (GetHeight ())),//dragview height log.v ("-->getleft ", String.valueof (GetLeft ())); Dragview the length of the left side of the screen log.v ("-->gettop", String.valueof (GetTop ()),///dragview the length of the top of the screen, LOG.V ("-->getrawx", The width of the itemview in the Dragview log.v ("-->getitemheight", String.valueof (Mtouchitemview.getheight ())); The height of the Itemview in the Dragview log.v ("-->getitemleft", String.valueof (Mtouchitemview.getleft ())); The length of the left side of the Dragview to the left of Itemview ("-->getitemtop", String.valueof (Mtouchitemview.gettop ()) in Dragview; The length of the top edge of the Itemview Dragview in Dragview
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.