Android Draggable sort GridView Implementation

Source: Internet
Author: User

Frequent use of today's headlines, NetEase News students should be aware of to manage multiple channels of the draggable sort GridView, the following describes the implementation of the Draggridview can be dragged. The code on GitHub is Https://github.com/zhaoyu87/DragGridView, and the students who need it can download

Draggridview inherits from GridView, when long press Select item to drag, let go update gridview order:

1. Override Ontouchevent Response Drag event: Record pressed coordinates when pressed, update dragged view when dragging, update sort on release

2. Set Onitemlongclicklistener: Response long Press Select drag item, get selected item bitmap, add to window display

3. Get the selected item's bitmap by View.getdrawingcache () to draw the dragged view

4. Use WindowManager to add a view to the window to update the view display. About Windowmanagerservice to the window organization way, blog http://blog.csdn.net/luoshengyang/article/details/8498908 has introduced, can refer to.

5. Getx () in motionevent is relative to the container's coordinates, here is gridview;getrawx () is relative to the entire screen coordinates

The Draggridview is implemented as follows, with a more detailed explanation in the comments

  1 public class Draggridview extends GridView {2 private static final int drag_img_show = 1;  3 private static final int drag_img_not_show = 0;  4 private static final String Log_tag = "Draggridview";  5 private static final float amp_factor = 1.2f;  6 7/** Dragged view */8 private ImageView Dragimageview; 9 Private Windowmanager.layoutparams Dragimageviewparams; Ten private WindowManager WindowManager; One private Boolean Isviewondrag = false; 12 13/** Position of the previous drag * * + private int predraggedoverpositon = adapterview.invalid_position; -Private int downrawx; + private int Downrawy;  17 18/** Long Press to select drag item*/private Onitemlongclicklistener onlongclicklistener = new Onitemlongclicklistener () {20 @Override 22//Long press item to start dragging the Onitemlongclick public boolean (adapterview<?> parent, View  view, int position, long ID) {24 25//record long press item position: Predraggedoverpositon = position; 2728//Get drawing Cache View.destroydrawingcache () that is long pressed on item; View.setdrawingcacheenabled (TRUE);  31//By long pressing the item, get Bitmap of drag item Bitmap Dragbitmap = Bitmap.createbitmap (View.getdrawingcache ()); 33 34//Set the parameter to drag item dragimageviewparams.gravity = Gravity.top | Gravity.left; 36//Set drag item for original item 1.2 times times PNS dragimageviewparams.width = (int) (Amp_factor*dragbitmap.getwidth ()); Dragimageviewparams.height = (int) (Amp_factor*dragbitmap.getheight ()); 39//Set touch point for drawing drag center of item dragimageviewparams.x = (DOWNRAWX-DRAGIMAGEVIEWPARAMS.WIDTH/2); Dragimageviewparams.y = (DOWNRAWY-DRAGIMAGEVIEWPARAMS.HEIGHT/2);                                         Dragimageviewparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 43 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 44 | WindowmAnager. LAYOUTPARAMS.FLAG_KEEP_SCREEN_ON 45 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; Dragimageviewparams.format = pixelformat.translucent; dragimageviewparams.windowanimations = 0; //dragimageview is the container for the item being dragged, emptying the last display of (int) dragimageview.gettag () = = Drag_img_show) {5 1 Windowmanager.removeview (Dragimageview); Dragimageview.settag (drag_img_not_show); 53} 54 55//Set the item Dragimageview.setimagebitmap (DRAGBITMAP) that was long pressed; 57 58//Add Drag Item to screen Windowmanager.addview (Dragimageview, dragimageviewparams); Dragimageview.settag (drag_img_show); Isviewondrag = true; 62 63//setting is long pressed item does not display ((Gridviewadapter) Getadapter ()). Hideview (position); N/a return true; 66} 67}; Draggridview (Context contEXT) {Initview (context); (); "Draggridview" (context context, attribut ESet attrs (context, attrs), Initview (), Draggridview (context con     Text, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); Bayi Initview (); 82 } N/a public void Initview () {Setonitemlongclicklistener (Onlongclicklistener); 86//initialization display dragged I The image view of tem Dragimageview = new ImageView (GetContext ()); Dragimageview.settag (drag_img_not_show); 89//Initialize Parameter object for setting Dragimageview Dragimageviewparams = new Windowmanager.layoutparams (); 91//Gets the window management object used to add Dragimageview WindowManager = (WindowManager) getcontext () after facing the window. Getsystemservice (Con Text. Window_service);         94 @Override ontouchevent (Motionevent ev) {98 99//record pressed coordinates 100 when pressed if (ev.getactIon () = = Motionevent.action_down) {101//Gets the coordinates of the touch point relative to the screen 102 DOWNRAWX = (int) ev.getrawx (); 103 Downrawy = (int) Ev.getrawy (); 104}105//dragimageview is being dragged, update Dragimageview position 106 else if (EV. Getaction () = = Motionevent.action_move) && (Isviewondrag = True)) {107 log.i (Log_tag, "" + ev.getrawx  () + "" + Ev.getrawy ()); 108//Set Touch point to Dragimageview Center 109 dragimageviewparams.x = (int) (EV.GETRAWX () -Dragimageview.getwidth ()/2); dragimageviewparams.y = (int) (Ev.getrawy ()-Dragimageview.getheight ()/2); 1             11//Update window showing Windowmanager.updateviewlayout (Dragimageview, dragimageviewparams); 113             Gets the current touch point of item position114 int currdraggedposition = pointtoposition ((int) ev.getx (), (int) ev.gety ()); 115  If the current stop position item is not equal to the last stop position of item, swap this and last stop item116 if ((currdraggedposition! = adapterview.invalid_position) && (currdraggedPosition! = Predraggedoverpositon)) {117 ((Gridviewadapter) Getadapter ()). Swapview (Predraggedoverpositon, C         Urrdraggedposition); 118 Predraggedoverpositon = currdraggedposition;119}120}121              Release dragImageView122 else if ((ev.getaction () = = motionevent.action_up) && (Isviewondrag = true)) {123 ((Gridviewadapter) Getadapter ()). Showhideview (); 124 if ((int) dragimageview.gettag () = = Drag_img_sho W) {Windowmanager.removeview (Dragimageview); 126 Dragimageview.settag (drag_img_not_show ); 127}128 Isviewondrag = false;129}130 return super.ontouchevent (EV); 131}13 2}

Gridviewadapter inherited from Baseadapter:

1. Provide Showhideview, Hideview two methods for displaying and hiding the text of the selected item

2. Provide the Swapview method for dragging during the update of the GridView item display

 1 public class Gridviewadapter extends Baseadapter {2 private context context; 3 Private list<string> StrL Ist 4 private int hideposition = adapterview.invalid_position;         5 6 Public Gridviewadapter (context context, list<string> strlist) {7 This.context = context; 8 This.strlist = strlist;     9}10 @Override12 public int GetCount () {return strlist.size ();}15 @Override17 Public String getItem (int position) {Strlist.get (position),}20 @Override22 public lon G Getitemid (int position) {position;24}25 @Override27 public View getView (int position, View Convertview, ViewGroup parent) {TextView view;29 if (Convertview = = null) {In view = NE W TextView (context);}32 else {view = (TextView) convertview;34}35 IDE when hidden Text37 if (position! = hideposition) {38             View.settext (Strlist.get (position));}40 else {view.settext (""); 42} View.setid (position), view;46}47, public void Hideview (int pos) {$ hide Position = Pos;50 notifydatasetchanged ();}52 (n) public void Showhideview () {hideposition = A         Dapterview.invalid_position;55 notifydatasetchanged ();}57, Removeview (int pos) {59 Strlist.remove (POS); Notifydatasetchanged () 61}62 63//Update when dragging gridView64 public void Swapview (int Draggedpos, int destpos) {65///Drag from front to back, other item move forward with the IF (Draggedpos < Destpos) {Strlist.add         (Destpos+1, GetItem (Draggedpos)); Strlist.remove (Draggedpos); 69}70//Drag forward from back, other item moves back 71 else if (Draggedpos > Destpos) {strlist.add (Destpos, GetItem (Draggedpos)); strlist.re   Move (draggedpos+1); 74      }75 hideposition = destpos;76 notifydatasetchanged (); 77}78} 

Android draggable sort GridView Implementation

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.