Objective
Online listview Drag up and down examples have, the effect is also very good, but the project should be dragged sideways, as long as the bite the bullet oneself write (mainly not found suitable), reference article 1 modified and come, share.
Screenshots
Code
Java
public class Hodragactivity extends activity {private LinearLayout main;
Private Gesturedetector Mgesturedetector;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.MAIN_CC);
Main = (linearlayout) Findviewbyid (R.id.main);
Binddraplistener (R.ID.MYIMAGE1);
Binddraplistener (R.id.myimage2);
Binddraplistener (R.id.myimage3);
Binddraplistener (R.ID.MYIMAGE4);
Binddraplistener (R.ID.MYIMAGE5);
Binddraplistener (R.id.myimage6);
Binddraplistener (R.id.myimage7);
Binddraplistener (R.id.myimage8);
Binddraplistener (R.ID.MYIMAGE9);
Binddraplistener (R.ID.MYIMAGE10);
Binddraplistener (R.ID.MYIMAGE11);
Binddraplistener (R.ID.MYIMAGE12);
Mgesturedetector = new Gesturedetector (This, new Drapgesturelistener ());
Private View Mdrapview; private void Binddraplistener (int id) {View v = Findviewbyid (id);
V.setontouchlistener (Montouchlistener);
V.setondraglistener (Mondraglistener); Private Ontouchlistener Montouchlistener = new Ontouchlistener () {@Override public Boolea
N Ontouch (View V, motionevent event) {Mdrapview = v;
if (Mgesturedetector.ontouchevent (event)) return true; Switch (event.getaction () & Motionevent.action_mask) {case MOTIONEVENT.ACTION_UP:BR
Eak
return false;
}
}; Private Ondraglistener Mondraglistener = new Ondraglistener () {@Override public boolean Ondrag View
V, dragevent event) {switch (event.getaction ()) {case dragevent.action_drag_started:
Do no break;
Case dragevent.action_drag_entered: V.setalpha (0.5F);
Break
Case Dragevent.action_drag_exited:v.setalpha (1F);
Break
Case DragEvent.ACTION_DROP:View view = (view) event.getlocalstate ();
for (int i = 0, j = main.getchildcount (); i < J; i++) {if (Main.getchildat (i) = v) {
Current position main.removeview (view);
Main.addview (view, i);
Break
}} break;
Case Dragevent.action_drag_ended:v.setalpha (1F);
Default:break;
return true;
}
}; Private class Drapgesturelistener extends Simpleongesturelistener {@Override public boolean Onsingletapco
Nfirmed (Motionevent e) {return super.onsingletapconfirmed (e); } @OveRride public void onlongpress (Motionevent e) {super.onlongpress (e);
Clipdata data = Clipdata.newplaintext ("", "");
Mydragshadowbuilder Shadowbuilder = new Mydragshadowbuilder (Mdrapview);
Mdrapview.startdrag (data, Shadowbuilder, Mdrapview, 0);
@Override public boolean Ondown (Motionevent e) {return true; } private class Mydragshadowbuilder extends View.dragshadowbuilder {private final Weakreferen
Ce<view> Mview;
Public Mydragshadowbuilder (view view) {super (view);
Mview = new weakreference<view> (View);
@Override public void Ondrawshadow (Canvas Canvas) {Canvas.scale (1.5F, 1.5F);
Super.ondrawshadow (canvas); @Override public void Onprovideshadowmetrics (Point shadowsize, point ShadowtoucHpoint) {//Super.onprovideshadowmetrics (Shadowsize, shadowtouchpoint);
Final View view = Mview.get (); if (view!= null) {shadowsize.set (int) (View.getwidth () * 1.5F), (int) (View.get
Height () * 1.5F));
Shadowtouchpoint.set (SHADOWSIZE.X/2, SHADOWSIZE.Y/2);
else {//LOG.E (View.view_log_tag,//"Asked for drag thumb metrics no View"); }
}
}
}
Code description
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/
1, Mydragshadowbuilder is mainly used to zoom drag-and-drop imageview, here magnified 1.5 times times, without affecting the original control, here is magnified twice times
2, here is through the long press to arouse drag and drop operation, so listen to the onlongpress
3, notice drag Ondraglistener is Android 3.0 only added API
Download
Vogelladraganddrop.zip
End
Welcome to the Exchange! Want to make drag insert when the automatic side of the effect of extrusion, did not come out, is the effect of jitter.
Author: cnblogs Peasant Uncle