Android Drag and Drop GridView -
i trying impement androids drag , drop on gridview. set draglistener each view in grid adapter. works when grid isn't long enough scrollable or if scrollable long first view in grid visible. when scrolled moving actions down position many views not visible @ top instead of being @ correct position.
gridview set draglistener inside of adapter:
imageview = new imageview; i.setimageresource(imagelist.get(position)); i.setondraglistener(new mydragimagelistener(position)); return i;
mydraglistener:
public class mydragimagelistener implements ondraglistener{ int imageposition; public mydragimagelistener(int position) { imageposition = position; } @override public boolean ondrag(view v, dragevent event) { switch (event.getaction()) { case dragevent.action_drag_started: log.i("drag started", "" + imageposition); break; case dragevent.action_drag_entered: log.i("drag entered", "" + imageposition); imageview imageenter = (imageview) grid.getchildat(imageposition); imageenter.setbackgroundcolor(color.argb(155, 100, 200, 255)); break; case dragevent.action_drag_exited: log.i("drag exited", "" + imageposition); imageview imageexit = (imageview) grid.getchildat(imageposition); imageexit.setbackgroundcolor(color.argb(0, 0, 0, 0)); break; case dragevent.action_drop: log.i("drag dropped", "" + imageposition); break; case dragevent.action_drag_ended: log.i("drag ended", "" + imageposition); default: break; } return true; } }
how handling touch events? there thin balance between offering touch actions in 1 single place:
@override public boolean onintercepttouchevent(motionevent ev) { if (mdragcontroller == null || !mdragging) { if (mdragcontroller!=null) mdragcontroller.onintercepttouchevent(ev); if (mswipeenabled) { gesturedetector.ontouchevent(ev); return false; //to make sure receive further messages } else return super.onintercepttouchevent(ev); //for scroll, etc } else // controller handle event return mdragcontroller.onintercepttouchevent(ev); } /** * */ @override public boolean ontouchevent(motionevent ev) { if (mdragcontroller == null || !mdragging) { //if (!gesturedetector.ontouchevent(ev)) return super.ontouchevent(ev); } // controller handle event return mdragcontroller.ontouchevent(ev); }
this works great, providing results illustrated in demo video: http://www.youtube.com/watch?v=m4yktx3swss (source code link in video description).
Comments
Post a Comment