Android gridview drag item

Source: Internet
Author: User
Tags gety

Public class draggridview extends gridview
{
Private string tag = "draggridview ";

Private int dragposition; // The position to start dragging.

Private int dropposition; // The Position of the end drag

Private int dragpointx; // The X coordinate relative to the item

Private int dragpointy; // y coordinate relative to item

Private int dragoffsetx;

Private int dragoffsety;

Private imageview dragimageview; // drag the preview of the item

Private windowmanager;

Private windowmanager. layoutparams windowparams;

Public draggridview (context)
{
Super (context );
}

Public draggridview (context, attributeset attrs)
{
Super (context, attrs );
}

@ Override
Public Boolean onintercepttouchevent (motionevent eV)
{
Log. D (TAG, "onintercepttouchevent" + super. onintercepttouchevent (EV ));
If (EV. getaction () = motionevent. action_down)
{
Int x = (INT) eV. getx ();
Int y = (INT) eV. Gety ();
Log. D (TAG, "x =" + x + "getrawx =" + eV. getrawx ());
Log. D (TAG, "Y =" + Y + "getrawy =" + eV. getrawy ());
Dragposition = dropposition = pointtoposition (x, y );
Log. I ("tag", "dragposition =" + dragposition );
If (dragposition = adapterview. invalid_position)
{
Return super. onintercepttouchevent (EV );
}
Log. D (TAG, "getfirstposition" + getfirstvisibleposition ());
View itemview = (View) getchildat (dragposition-getfirstvisibleposition ());
// Obtain the offset of the current point in the item, that is, the coordinate relative to the upper left corner of the item.
Log. D (TAG, "itemview. getleft =" + itemview. getleft () + ", itemview. gettop =" + itemview. gettop ());

Dragpointx = x-itemview. getleft (); // obtain the coordinates of the point to the left of the item.
Dragpointy = Y-itemview. gettop ();

// Coordinates in the upper left corner of the item. (x, y)
Dragoffsetx = (INT) (EV. getrawx ()-x );
Dragoffsety = (INT) (EV. getrawy ()-y );

Log. D (TAG, "dragpointx =" + dragpointx + ", dragpointy =" + dragpointy + ", dragoffsetx =" + dragoffsetx
+ ", Dragoffsety =" + dragoffsety );

// Solve problem 3
// Destroy the cache once each time and regenerate a bitmap
Itemview. destroydrawingcache ();
Itemview. setdrawingcacheenabled (true );
Bitmap Bm = bitmap. createbitmap (itemview. getdrawingcache ());

// Create a thumbnail of the item
Startdrag (BM, x, y );
Return false;
}
Return super. onintercepttouchevent (EV );
}

Private void startdrag (Bitmap BM, int X, int y)
{
Stopdrag ();
Windowparams = new windowmanager. layoutparams ();
Log. I ("xqqlog", "X:" + x + "dragpointx:" + dragpointx + "dragoffsetx:" + dragoffsetx );
Windowparams. Gravity = gravity. Top | gravity. Left; // This parameter must be added.
// Obtain the coordinates of the upper left corner of the preview relative to the screen.
Windowparams. x = x-dragpointx + dragoffsetx;
Windowparams. Y = Y-dragpointy + dragoffsety;
// Set the width and height
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;

}

@ Override
Public Boolean ontouchevent (motionevent eV)
{
If (dragimageview! = NULL & dragposition! = Adapterview. invalid_position)
{

Switch (EV. getaction ())
{
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 ();
Log. D ("action_move", "action_move X" + movex + "Ev. getrawx =" + eV. getrawx ());
Ondrag (movex, Movey );
Break;

}
}
Return super. ontouchevent (EV );
}

Private void ondrag (int x, int y)
{
If (dragimageview! = NULL)
{
Windowparams. Alpha = 0.6f;

// X indicates the layout coordinate of the view relative to the edge.
// Dragpointx indicates the position on the left of the item
// Dragoffsetx indicates the position of the layout relative to the screen (0, 0)

// X-dragpointx = getleft ();
// Y-dragponity = gettop ();
Windowparams. x = x-dragpointx + dragoffsetx;
Windowparams. Y = Y-dragpointy + dragoffsety;
Windowmanager. updateviewlayout (dragimageview, windowparams );
}
}

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 );

// Solve problem 3
/*
* Viewgroup itemview1 = (viewgroup) getchildat (dragposition-
* Getfirstvisibleposition (); viewgroup itemview2 =
* (Viewgroup) getchildat (dropposition-getfirstvisibleposition ());
* Itemview1.destroydrawingcache (); itemview2.destroydrawingcache ();
*/
}
}

Private void stopdrag ()
{
If (dragimageview! = NULL)
{
Windowmanager. removeview (dragimageview );
Dragimageview = NULL;
}

}

}

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.