android - Drag and Drop not working in Fragment -
the code working in activity
, not in fragment
. can see elements there, can't drag , drop them, stops in long click.
can provide solution?
error log:
*e/androidruntime: fatal exception: main process: com.example.future.buildingautomationsystem, pid: 10526 java.lang.nullpointerexception @ com.example.future.buildingautomationsystem.slidefragment.onitemlongclick(slidefragment.java:179) @ android.widget.abslistview.performlongpress(abslistview.java:3175) @ android.widget.abslistview$checkforlongpress.run(abslistview.java:3124) @ android.os.handler.handlecallback(handler.java:808) @ android.os.handler.dispatchmessage(handler.java:103) @ android.os.looper.loop(looper.java:193) @ android.app.activitythread.main(activitythread.java:5395) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:837) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:653) @ dalvik.system.nativestart.main(native method)*
code:
import java.util.arraylist; import android.app.activity; import android.app.fragment; import android.content.clipdata; import android.content.clipdescription; import android.os.bundle; import android.os.handler; import android.view.dragevent; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.view.ondraglistener; import android.view.view.onlongclicklistener; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemlongclicklistener; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imageview; public class slidefragment extends fragment implements ondraglistener, onitemlongclicklistener { arraylist drawables; private baseadapter adapter; private int draggedindex = -1; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub view v; v = inflater.inflate(r.layout.slidefragment, container,false); super.oncreate(savedinstancestate); drawables = new arraylist(); drawables.add(r.drawable.ico1); drawables.add(r.drawable.ico1); drawables.add(r.drawable.ico1); drawables.add(r.drawable.ico1); drawables.add(r.drawable.ico1); drawables.add(r.drawable.ico1); drawables.add(r.drawable.ico1); drawables.add(r.drawable.ico1); drawables.add(r.drawable.ico1); gridview gridview = (gridview) v.findviewbyid(r.id.grid_view); gridview.setonitemlongclicklistener(slidefragment.this); gridview.setadapter(adapter = new baseadapter() { @override // view displays data @ specified position in // data set. public view getview(int position, view convertview, viewgroup gridview) { // try reuse views. imageview view = (imageview) convertview; // if convert view null create new instance else reuse // if (view == null) { view = new imageview(getactivity().getbasecontext()); } view.setimageresource((integer) drawables.get(position)); view.settag(string.valueof(position)); return view; } @override // row id associated specified position in // list. public long getitemid(int position) { return position; } @override // data item associated specified position in // data set. public object getitem(int position) { return drawables.get(position); } @override // how many items in data set represented adapter. public int getcount() { return drawables.size(); } }); return v; } // @override // public boolean oncreateoptionsmenu(menu menu) { // getmenuinflater().inflate(r.menu.menu_login,getview().menu); // return true; //} @override public boolean ondrag(final view view, dragevent dragevent) { switch (dragevent.getaction()) { case dragevent.action_drag_started: // drag has started // if called trash resize view , return true if (view.getid() == r.id.droplayout) { view.animate().scalex(1.0f); view.animate().scaley(1.0f); return true; } else // else check mime type , set view visibility if (dragevent.getclipdescription().hasmimetype( clipdescription.mimetype_text_plain)) { view.setvisibility(view.gone); return true; } else { return false; } case dragevent.action_drag_entered: // drag has entered view bounds // if called trash can scale it. if (view.getid() == r.id.droplayout) { view.animate().scalex(1.5f); view.animate().scaley(1.5f); } return true; case dragevent.action_drag_exited: // drag exited view bounds // if called trash can reset it. if (view.getid() == r.id.droplayout) { view.animate().scalex(1.0f); view.animate().scaley(1.0f); } view.invalidate(); return true; case dragevent.action_drag_location: // ignore event return true; case dragevent.action_drop: // dropped inside view bounds // if called trash can delete item , reload grid // view if (view.getid() == r.id.droplayout) { drawables.remove(draggedindex); draggedindex = -1; } adapter.notifydatasetchanged(); case dragevent.action_drag_ended: // hide trash can new handler().postdelayed(new runnable() { @override public void run() { view. findviewbyid(r.id.droplayout).setvisibility(view.gone); } }, 1000l); if (view.getid() == r.id.droplayout) { view.animate().scalex(1.0f); view.animate().scaley(1.0f); } else { view.setvisibility(view.visible); } // remove drag listeners view.setondraglistener(null); return true; } return false; } @override public boolean onitemlongclick(adapterview gridview, view view, int position, long row) { clipdata.item item = new clipdata.item((string) view.gettag()); clipdata clipdata = new clipdata((charsequence) view.gettag(), new string[] { clipdescription.mimetype_text_plain }, item); view.startdrag(clipdata, new view.dragshadowbuilder(view), null, 0); view trashcan = view.findviewbyid(r.id.linearlayoutbottom); trashcan.setvisibility(view.visible); trashcan.setondraglistener(slidefragment.this); trashcan.setondraglistener(slidefragment.this); draggedindex = position; return true; } public layoutinflater getmenuinflater() { return getmenuinflater(); } }
Comments
Post a Comment