Gridview long press drag item effect (Android)

Source: Internet
Author: User
Tags gety

1. The project requires the effect of long-pressed and drag-and-drop.

There is a copy of code on the network, there are two unsolved problems:

1) Click and drag, and the interface will be disturbed when you move down, because he uses action. Move

2) When the dragged view quickly approaches the edge of the mobile phone screen, the gridview does not move down.

2. After modification, the drag-and-drop interface is displayed after a long press. The Code is as follows:

Package COM. PL; import COM. pl. mainactivity. imageadapter; import android. content. context; import android. graphics. bitmap; import android. graphics. pixelformat; import android. util. attributeset; import android. view. gravity; import android. view. motionevent; import android. view. view; import android. view. viewgroup; import android. view. windowmanager; import android. widget. adapterview; import android. widget. gridview; I Mport android. widget. imageview; public class draggridview extends gridview {private intdragposition; // The start drag position private intdropposition; // The end drag position private intdragpointx; // The X coordinate private intdragpointy relative to the item; // relative to the Y coordinate of the item, private intdragoffsetx; private intdragoffsety; private imageviewdragimageview; // drag the previewprivate windowmanagerwindowmanager of the item; private windowmanager. layoutparamswindowparams; private in Titemheight; Public draggridview (context, attributeset attrs) {super (context, attrs);} booleanflag = false; Public void setlongflag (Boolean temp) {flag = temp ;} public Boolean setonitemlongclicklistener (final motionevent eV) {This. setonitemlongclicklistener (New adapterview. onitemlongclicklistener () {@ overridepublic Boolean onitemlongclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// onintercepttouchevent (EV); // todo auto-generated method stubl. L ("=" Long click = "); L. L ("=" + eV. getx () + "Y:" + eV. gety (); int x = (INT) eV. getx (); int y = (INT) eV. gety (); L. L ("============== X:" + x + "Y:" + Y); dragposition = dropposition = pointtoposition (x, y ); system. out. println (dragposition); If (dragposition = adapterview. I Nvalid_position) {} viewgroup itemview = (viewgroup) getchildat (dragposition-getfirstvisibleposition (); // get the offset of the current point in the item, that is, the coordinate dragpointx = x-itemview relative to the upper left corner of the item. getleft (); dragpointy = Y-itemview. gettop (); dragoffsetx = (INT) (ev. getrawx ()-x); dragoffsety = (INT) (ev. getrawy ()-y); itemheight = itemview. getheight (); L. L ("==================================== Y:" + Y + "getrawy:" + eV. getrawy (); // solve the problem 3 // destroy each time Cache once to regenerate a bitmapitemview. destroydrawingcache (); itemview. setdrawingcacheenabled (true); bitmap Bm = bitmap. createbitmap (itemview. getdrawingcache (); // create the item thumbnail startdrag (BM, x, y); Return false ;}}); return Super. onintercepttouchevent (EV);} @ overridepublic Boolean onintercepttouchevent (motionevent eV) {If (ev. getaction () = motionevent. action_down) {return setonitemlongclicklistener (EV);} return Super. Onintercepttouchevent (EV);} private void startdrag (Bitmap BM, int X, int y) {stopdrag (); windowparams = new windowmanager. layoutparams (); system. out. println ("X:" + x + "dragpointx:" + dragpointx + "dragoffsetx:" + dragoffsetx); windowparams. gravity = gravity. top | gravity. left; // This parameter must be added. // the upper-left corner of the preview corresponds to the screen coordinate windowparams. X = x-dragpointx + dragoffsetx; windowparams. y = Y-dragpointy + dragoffsety; // L. L ("=========================== windowparams. y = "+ windowparams. y); // set the width and height of windowparams. height = windowmanager. layoutparams. wrap_content; windowparams. width = windowmanager. layoutparams. wrap_content; windowparams. flags = windowmanager. layoutparams. flag_not_focusable | windowmanager. layoutparams. flag_not_touchable | windowmanager. layoutparams. flag_keep_screen_on | windowmanager. layoutparams. flag _ Layout_in_screen; windowparams. format = pixelformat. translucent; windowparams. windowanimations = 0; imageview IV = new imageview (getcontext (); IV. setimagebitmap (BM); windowmanager = (windowmanager) getcontext (). getsystemservice (context. window_service); // "window" windowmanager. addview (IV, windowparams); dragimageview = IV ;}@ overridepublic Boolean ontouchevent (motionevent eV) {If (dragimageview! = NULL & dragposition! = Adapterview. invalid_position) {int x = (INT) eV. getx (); int y = (INT) eV. gety (); Switch (ev. getaction () {Case motionevent. action_move: ondrag (x, y); break; Case motionevent. action_up: stopdrag (); ondrop (x, y); break;} return Super. ontouchevent (EV);} private void ondrag (int x, int y) {If (dragimageview! = NULL) {windowparams. alpha = 0.6f; windowparams. X = x-dragpointx + dragoffsetx; windowparams. y = Y-dragpointy + dragoffsety; // L. L ("========================= windowparams. y = 000 = "+ windowparams. y); windowmanager. updateviewlayout (dragimageview, windowparams);} int tempscrollx = x-dragpointx + dragoffsetx; int tempscrolly = Y-dragpointy + dragoffsety; If (tempscrolly + itemheight> 600) {This. scrollto (0, tempscrolly);} elseif (pointtoposition (x, y)> 2) {This. scrollto (0, tempscrolly-300);} private void ondrop (int x, int y) {int tempposition = pointtoposition (x, y); If (tempposition! = Adapterview. invalid_position) {dropposition = tempposition;} If (dropposition! = Dragposition) {system. out. println ("dragposition:" + dragposition + "dropposition:" + dropposition); imageadapter adapter = (imageadapter) This. getadapter (); adapter. exchange (dragposition, dropposition); // solution 3/** viewgroup itemview1 = (viewgroup) getchildat (dragposition-getfirstvisibleposition (); viewgroup * itemview2 = (viewgroup) getchildat (dropposition-getfirstvisibleposition (); * itemvie W1.destroydrawingcache (); itemview2.destroydrawingcache (); */} private void stopdrag () {If (dragimageview! = NULL) {windowmanager. removeview (dragimageview); dragimageview = NULL ;}}}

Package COM. PL; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. baseadapter; import android. widget. imageview; import android. widget. textview; import Java. util. arraylist; Import Java. util. list; public class mainactivity extends activity {private draggridview GV; private list <string> List = new arraylist <string> (); @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); initdata (); GV = (draggridview) findviewbyid (R. id. drag_grid); GV. setadapter (New imageadapter (this); GV. setonitemclicklistener (ne W onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view, int position, long ID) {// todo auto-generated method stub L. L ("============ onitemclick ==== position:" + position) ;}) ;} private void initdata () {for (INT I = 1; I <100; I ++) {list. add ("" + I) ;}} public class imageadapter extends baseadapter {private context mcontext; private layoutinflater LIF; Public imageadapter (context c) {mcontext = C; LIF = layoutinflater. from (c);} public int getcount () {return list. size ();} public object getitem (INT position) {// todo auto-generated method stub return list. get (position);} public long getitemid (INT position) {// todo auto-generated method stub return position;}/*** problems: * 1. If the image is deleted incorrectly, there will be repeated images * This is caused by removing * We first delete a small position * For example, startposition = 2; endposition = 3; * When I delete startposition first, the item with position 3 before deletion is already position 2*2. array out-of-bounds exception * This is caused by add * For example, startposition = 8; endposition = 7; * A total of 9 elements in the gridview, that is to say, 8 is already the largest. * When deletion is complete, you add startposition first. * 3. Preview problems * When I drag and drop to swap a few elements next, the current image Displays the preview of another image. ** call destroydrawingcache * @ Param startposition * @ Param endposition */Public void Exchange (INT startposition, int endposition) {// compare to make startposition always less than endposition to solve problem 1, 2 if (startposition> endposition) {int temp = endposition; endposition = startposition; startposition = temp;} object endobject = getitem (endposition); object startobject = getitem (startposition); // list. remove (endposition); // list. remove (startposition); system. out. println (startposition + "=======" + endposition); list. set (startposition, (string) endobject); list. set (endposition, (string) startobject); notifydatasetchanged ();} public view getview (INT position, view convertview, viewgroup parent) {// imageview IV; if (convertview = NULL) {convertview = lif. inflate (R. layout. grid_item, null); // IV = new imageview (mcontext); // try {// field F = (field) R. drawable. class. getdeclaredfield (list. get (position); // int I = f. getint (R. drawable. class); // IV. setimageresource (I); //} catch (exception e) {// E. printstacktrace (); //} Try {textview = (textview) convertview. findviewbyid (R. id. textview); textview. settext (list. get (position); imageview imgview = (imageview) convertview. findviewbyid (R. id. image); imgview. setbackgroundresource (R. drawable. icon);} catch (exception e) {e. printstacktrace () ;}return convertview ;}}}

3. Download the complete code as follows:

Http://download.csdn.net/detail/synwith/3693769

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.