Android Learning Series (12)-drag and drop the App list gridview

Source: Internet
Author: User
Tags gety
According to the preceding Article In the listview drag implementation principle, we are also very easy to pull the gridview, the following is the same steps to achieve the basic gridview drag effect.
Because the gridview does not need to be grouped, Code The process is more concise, and the Principles have been explained clearly before. The code is just a simple one, and the necessary points are briefly commented out.
1. Main Interface draggridactivity.
Public class draggridactivity extends activity {Private Static list <string> List = NULL; // custom adapter private draggridadapter adapter = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. drag_grid_activity); initdata (); // The Custom gridview draggridview = (draggridview) findviewbyid (R. id. drag_grid); adapter = new draggridadapter (this, list); draggridview. setadapter (adapter);} public void initdata () {// data result list = new arraylist <string> (); For (INT I = 0; I <12; I ++) {list. add ("Grid _" + I % 12 );}}}

2. Main UI layout drag_grid_activity.xml.

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Background = "# ffffff" Android: padding = "10dip"> <COM. fengjian. test. draggridview Android: Id = "@ + ID/drag_grid" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: cachecolorhint = "#00000000" Android: numcolumns = "3" Android: stretchmode = "columnwidth" Android: verticalspacing = "5dip" Android: horizontalspacing = "20dip" Android: Background = "# ffffff"/> </linearlayout>

3. list item layout drag_grid_item.xml.

<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: paddingleft = "5dip" Android: paddingright = "5dip"> <imageview Android: Id = "@ + ID/drag_grid_item_image" Android: src = "@ drawable/grid_icon" Android: layout_margin = "5dip" Android: layout_alignparenttop = "true" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/drag_grid_item_drag" Android: src = "@ drawable/grid_drag" Android: layout_alignparenttop = "true" Android: layout_alignparentright = "true" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </relativelayout>

4. Custom adapter draggridadapter, inherited from arrayadapter <string>.

Public static class draggridadapter extends arrayadapter <string> {public draggridadapter (context, list <string> objects) {super (context, 0, objects );} public list <string> getlist () {return list;} @ override public view getview (INT position, view convertview, viewgroup parent) {view = convertview; if (view = NULL) {view = layoutinflater. from (getcontext ()). inflate (R. layout. drag_grid_item, null);} Try {// obtain the image resource field F = (field) R in the resource folder Based on the file name. drawable. class. getdeclaredfield (getitem (position); int I = f. getint (R. drawable. class); imageview = (imageview) view. findviewbyid (R. id. drag_grid_item_image); imageview. setimageresource (I);} catch (securityexception e) {e. printstacktrace ();} catch (nosuchfieldexception e) {e. printstacktrace ();} catch (illegalargumentexception e) {e. printstacktrace ();} catch (illegalaccessexception e) {e. printstacktrace () ;}return view ;}}

5. The custom View class draggridview inherits the gridview.

 
Public class draggridview extends gridview {// defines the basic member variable private imageview dragimageview; private int dragsrcposition; private int dragposition; // calculates private int dragpointx and Y coordinates; private int dragpointy; private int dragoffsetx; private int dragoffsety; private windowmanager. layoutparams windowparams; private int scaledtouchslop; private int upscrollbounce; private int downscrollbounce; Public draggridview (context, attributeset attrs) {super (context, attrs );}}

6.
Override the touch interception event Method onintercepttouchevent ().

@ Override public Boolean onintercepttouchevent (motionevent eV) {If (ev. getaction () = motionevent. action_down) {int x = (INT) eV. getx (); int y = (INT) eV. gety (); dragsrcposition = dragposition = pointtoposition (x, y); If (dragposition = adapterview. invalid_position) {return Super. onintercepttouchevent (EV);} viewgroup itemview = (viewgroup) getchildat (dragposition-getfirstvisibleposition (); dragpointx = X-itemview. getleft (); dragpointy = Y-itemview. gettop (); dragoffsetx = (INT) (ev. getrawx ()-x); dragoffsety = (INT) (ev. getrawy ()-y); view dragger = itemview. findviewbyid (R. id. drag_grid_item_drag); // if you select the drag icon if (dragger! = NULL & dragpointx> dragger. getleft () & dragpointx <dragger. getright () & dragpointy> dragger. gettop () & dragpointy <dragger. getbottom () + 20) {upscrollbounce = math. min (Y-scaledtouchslop, getheight ()/4); downscrollbounce = math. max (Y + scaledtouchslop, getheight () * 3/4); itemview. setdrawingcacheenabled (true); bitmap Bm = bitmap. createbitmap (itemview. getdrawingcache (); startdrag (BM, x, y);} return false;} return Super. onintercepttouchevent (EV );}
The startdrag and stopdrag methods are as follows:
Public void startdrag (Bitmap BM, int X, int y) {stopdrag (); windowparams = new windowmanager. layoutparams (); windowparams. gravity = gravity. top | gravity. left; windowparams. X = x-dragpointx + dragoffsetx; windowparams. y = Y-dragpointy + dragoffsety; windowparams. width = windowmanager. layoutparams. wrap_content; windowparams. height = windowmanager. layoutparams. wrap_content; windowparams. flags = Win Dowmanager. 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 = new imageview (getcontext (); imageview. setimagebitmap (BM); windowmanager = (windowmanager) getcontext (). g Etsystemservice ("window"); windowmanager. addview (imageview, windowparams); dragimageview = imageview;} public void ondrag (int x, int y) {If (dragimageview! = NULL) {windowparams. alpha = 0.8f; windowparams. X = x-dragpointx + dragoffsetx; windowparams. y = Y-dragpointy + dragoffsety; windowmanager. updateviewlayout (dragimageview, windowparams);} int tempposition = pointtoposition (x, y); If (tempposition! = Invalid_position) {dragposition = tempposition;} // scroll if (Y <upscrollbounce | Y> downscrollbounce) {// use setselection to implement rolling setselection (dragposition );}}

7. Override the ontouchevent () method.

 
@ Override public Boolean ontouchevent (motionevent eV) {If (dragimageview! = NULL & dragposition! = Invalid_position) {int action = eV. getaction (); Switch (Action) {Case motionevent. action_up: int UPX = (INT) eV. getx (); int upy = (INT) eV. gety (); stopdrag (); ondrop (UPX, upy); break; Case motionevent. action_move: int movex = (INT) eV. getx (); int Movey = (INT) eV. gety (); ondrag (movex, Movey); break; default: break;} return true;} return Super. ontouchevent (EV );}

The ondrag method is as follows:

Public void ondrag (int x, int y) {If (dragimageview! = NULL) {windowparams. alpha = 0.8f; windowparams. X = x-dragpointx + dragoffsetx; windowparams. y = Y-dragpointy + dragoffsety; windowmanager. updateviewlayout (dragimageview, windowparams);} int tempposition = pointtoposition (x, y); If (tempposition! = Invalid_position) {dragposition = tempposition;} // scroll if (Y <upscrollbounce | Y> downscrollbounce) {// use setselection to implement rolling setselection (dragposition );}}

8. Put down the image and update the data.
Implementation in the ondrop method:

Public void ondrop (int x, int y) {// in order to avoid sliding to the split line, the-1 problem int tempposition = pointtoposition (X, Y) is returned; If (tempposition! = Invalid_position) {dragposition = tempposition;} // If (Y <getchildat (0) when the boundary is exceeded ). gettop () {// exceeds the upper boundary dragposition = 0;} else if (Y> getchildat (getchildcount ()-1 ). getbottom () | (Y> getchildat (getchildcount ()-1 ). gettop () & x> getchildat (getchildcount ()-1 ). getright () {// exceeds the bottom boundary dragposition = getadapter (). getcount ()-1;} // If (dragposition! = Dragsrcposition & dragposition>-1 & dragposition <getadapter (). getcount () {draggridadapter adapter = (draggridadapter) getadapter (); string dragitem = adapter. getitem (dragsrcposition); adapter. remove (dragitem); adapter. insert (dragitem, dragposition); toast. maketext (getcontext (), adapter. getlist (). tostring (), toast. length_short ). show ();}}

10. The final result is as follows:

 

Figure 1

 

Figure 2

This article is a supplement and extension of the previous article.

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.