android - Implementing a class that extends a listview -
i have class dragging , re-arranging of list items how use in listactivity or other activity. new stuff , appreciated. here code please help.
package com.iconasystems.christo.dynamiclistview; import android.animation.animator; import android.animation.animatorlisteneradapter; import android.animation.objectanimator; import android.animation.typeevaluator; import android.animation.valueanimator; import android.content.context; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.rect; import android.graphics.drawable.bitmapdrawable; import android.util.attributeset; import android.util.displaymetrics; import android.view.motionevent; import android.view.view; import android.view.viewtreeobserver; import android.widget.abslistview; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.baseadapter; import android.widget.listview; import java.util.arraylist; import static android.widget.adapterview.onitemlongclicklistener; /** * created christo on 8/14/2014. */ public class dynamiclist extends listview { private final int smooth_scroll_amount_at_edge = 15; private final int move_duration = 150; private final int line_thickness = 15; public arraylist<string> marraylist; private int mlasteventy = -1; private int mdowny = -1; private int mdownx = -1; private int mtotaloffset = 0; private boolean mcellismobile = false; private boolean mismobilescrolling = false; private int msmoothscrollamountatedge = 0; private final int invalid_id = -1; private long maboveitemid = invalid_id; private long mmobileitemid = invalid_id; private long mbelowitemid = invalid_id; private bitmapdrawable mhovercell; private rect mhovercellcurrentbounds; private rect mhovercelloriginalbounds; private final int invalid_pointer_id = -1; private int mactivepointerid = invalid_pointer_id; private boolean miswaitingforscrollfinish = false; private int mscrollstate = onscrolllistener.scroll_state_idle; public dynamiclist(context context) { super(context); } public dynamiclist(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(context); } public dynamiclist(context context, attributeset attrs) { super(context, attrs); init(context); } public void init(context context) { setonitemclicklistener((onitemclicklistener) monitemlongclicklistener); setonscrolllistener(mscrolllistener); displaymetrics metrics = context.getresources().getdisplaymetrics(); msmoothscrollamountatedge = (int) (smooth_scroll_amount_at_edge / metrics.density); } private onitemlongclicklistener monitemlongclicklistener = new onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> parent, view view, int position, long id) { mtotaloffset = 0; position = pointtoposition(mdownx, mdowny); int itemnum = position - getfirstvisibleposition(); view selectedview = getchildat(itemnum); mmobileitemid = getadapter().getitemid(position); mhovercell = getandaddhoverview(selectedview); selectedview.setvisibility(invisible); mcellismobile = true; updateneighbourviewsforid(mmobileitemid); return true; } }; private bitmapdrawable getandaddhoverview(view v) { int w = v.getwidth(); int h = v.getheight(); int top = v.gettop(); int left = v.getleft(); bitmap b = getbitmapwithborder(v); bitmapdrawable drawable = new bitmapdrawable(getresources(), b); mhovercelloriginalbounds = new rect(left, top, left + w, top + h); mhovercellcurrentbounds = new rect(mhovercelloriginalbounds); drawable.setbounds(mhovercelloriginalbounds); return drawable; } private bitmap getbitmapwithborder(view v) { bitmap bitmap = getbitmapfromview(v); canvas can = new canvas(bitmap); rect rect = new rect(0, 0, bitmap.getwidth(), bitmap.getheight()); paint paint = new paint(); paint.setstyle(paint.style.stroke); paint.setstrokewidth(line_thickness); paint.setcolor(color.black); can.drawbitmap(bitmap, 0, 0, null); can.drawrect(rect, paint); return bitmap; } private bitmap getbitmapfromview(view v) { bitmap bitmap = bitmap.createbitmap(v.getwidth(), v.getheight(), bitmap.config.argb_8888); canvas canvas = new canvas(bitmap); v.draw(canvas); return bitmap; } private void updateneighbourviewsforid(long itemid) { int position = getpositionforid(itemid); arrayadapter adapter = ((arrayadapter) getadapter()); maboveitemid = adapter.getitemid(position - 1); mbelowitemid = adapter.getitemid(position + 1); } public view getviewforid(long itemid) { int firstvisibleposition = getfirstvisibleposition(); arrayadapter adapter = ((arrayadapter) getadapter()); (int = 0; < getchildcount(); i++) { view v = getchildat(i); int position = firstvisibleposition + 1; long id = adapter.getitemid(position); if (id == itemid) { return v; } } return null; } public int getpositionforid(long itemid) { view v = getviewforid(itemid); if (v == null) { return -1; } else { return getpositionforview(v); } } @override protected void dispatchdraw(canvas canvas) { super.dispatchdraw(canvas); if (mhovercell != null) { mhovercell.draw(canvas); } } @override public boolean ontouchevent(motionevent event) { switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: mdownx = (int) event.getx(); mdowny = (int) event.gety(); mactivepointerid = event.getpointerid(0); break; case motionevent.action_move: if (mactivepointerid == invalid_pointer_id) { break; } int pointerindex = event.findpointerindex(mactivepointerid); mlasteventy = (int) event.gety(pointerindex); int deltay = mlasteventy - mdowny; if (mcellismobile) { mhovercellcurrentbounds.offsetto(mhovercelloriginalbounds.left, mhovercelloriginalbounds.top + deltay + mtotaloffset); mhovercell.setbounds(mhovercellcurrentbounds); invalidate(); handlecellswitch(); mismobilescrolling = false; handlemobilecellscroll(); } break; case motionevent.action_up: toucheventsended(); break; } return false; } private void handlecellswitch() { final int deltay = mlasteventy - mdowny; int deltaytotal = mhovercelloriginalbounds.top + mtotaloffset + deltay; view belowview = getviewforid(mbelowitemid); view mobileview = getviewforid(mmobileitemid); view aboveview = getviewforid(maboveitemid); boolean isbelow = (belowview != null) && (deltaytotal > belowview.gettop()); boolean isabove = (aboveview != null) && (deltaytotal < aboveview.gettop()); if (isbelow || isabove) { final long switchitemid = isbelow ? maboveitemid : maboveitemid; view switchview = isbelow ? belowview : aboveview; final int originalitem = getpositionforview(mobileview); if (switchview == null) { updateneighbourviewsforid(mmobileitemid); return; } swapelements(marraylist, originalitem, getpositionforview(switchview)); ((baseadapter) getadapter()).notifydatasetchanged(); mdowny = mlasteventy; final int switchviewstarttop = switchview.gettop(); mobileview.setvisibility(view.visible); switchview.setvisibility(view.invisible); updateneighbourviewsforid(mmobileitemid); final viewtreeobserver observer = getviewtreeobserver(); observer.addonpredrawlistener(new viewtreeobserver.onpredrawlistener() { @override public boolean onpredraw() { observer.removeonpredrawlistener(this); view switchview = getviewforid(switchitemid); mtotaloffset += deltay; int switchviewnewtop = switchview.gettop(); int delta = switchviewstarttop - switchviewnewtop; switchview.settranslationy(delta); objectanimator animator = objectanimator.offloat(switchview, view.translation_y, 0); animator.setduration(move_duration); animator.start(); return true; } }); } } private void swapelements(arraylist arraylist, int indexone, int indextwo) { object temp = arraylist.get(indexone); arraylist.set(indexone, arraylist.get(indextwo)); arraylist.set(indextwo, temp); } private void toucheventsended() { final view mobileview = getviewforid(mmobileitemid); if (mcellismobile || miswaitingforscrollfinish) { mcellismobile = false; miswaitingforscrollfinish = false; mismobilescrolling = false; mactivepointerid = invalid_pointer_id; if (mscrollstate != onscrolllistener.scroll_state_idle) { miswaitingforscrollfinish = true; return; } mhovercellcurrentbounds.offsetto(mhovercelloriginalbounds.left, mobileview.gettop()); objectanimator hoverviewanimator = objectanimator.ofobject(mhovercell, "bounds", sboundevaluator, mhovercellcurrentbounds); hoverviewanimator.addupdatelistener(new valueanimator.animatorupdatelistener() { @override public void onanimationupdate(valueanimator animation) { invalidate(); } }); hoverviewanimator.addlistener(new animatorlisteneradapter() { @override public void onanimationstart(animator animation) { setenabled(false); } @override public void onanimationend(animator animation) { maboveitemid = invalid_id; mmobileitemid = invalid_id; mbelowitemid = invalid_id; mobileview.setvisibility(visible); mhovercell = null; setenabled(true); invalidate(); } }); hoverviewanimator.start(); } else { toucheventscancelled(); } } private void toucheventscancelled() { view mobileview = getviewforid(mmobileitemid); if (mcellismobile) { maboveitemid = invalid_id; mmobileitemid = invalid_id; mbelowitemid = invalid_id; mobileview.setvisibility(visible); mhovercell = null; invalidate(); } mcellismobile = false; mismobilescrolling = false; mactivepointerid = invalid_pointer_id; } private final static typeevaluator<rect> sboundevaluator = new typeevaluator<rect>() { @override public rect evaluate(float fraction, rect startvalue, rect endvalue) { return new rect(interpolate(startvalue.left, endvalue.left, fraction), interpolate(startvalue.top, endvalue.top, fraction), interpolate(startvalue.right, endvalue.right, fraction), interpolate(startvalue.bottom, endvalue.bottom, fraction)); } public int interpolate(int start, int end, float fraction) { return (int) (start + fraction * (end - start)); } }; private void handlemobilecellscroll() { mismobilescrolling = handlemobilecellscroll(mhovercellcurrentbounds); } public boolean handlemobilecellscroll(rect r) { int offset = computeverticalscrolloffset(); int height = getheight(); int extent = computeverticalscrollextent(); int range = computehorizontalscrollrange(); int hoverviewtop = r.top; int hoverheight = r.height(); if (hoverviewtop <= 0 && offset > 0) { smoothscrollby(-msmoothscrollamountatedge, 0); return true; } if (hoverviewtop + hoverheight >= height && (offset + extent) < range) { smoothscrollby(msmoothscrollamountatedge, 0); return true; } return false; } public void setarraylist(arraylist<string> arraylist) { marraylist = arraylist; } private onscrolllistener mscrolllistener = new onscrolllistener() { private int mpreviousfirstvisibleitem = -1; private int mpreviousvisibleitemcount = -1; private int mcurrentfirstvisibleitem; private int mcurrentvisibleitemcount; private int mcurrentscrollstate; @override public void onscrollstatechanged(abslistview view, int scrollstate) { mcurrentscrollstate = scrollstate; mscrollstate = scrollstate; isscrollcompleted(); } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { mcurrentfirstvisibleitem = firstvisibleitem; mcurrentvisibleitemcount = visibleitemcount; mpreviousfirstvisibleitem = (mpreviousfirstvisibleitem == -1) ? mcurrentfirstvisibleitem : mpreviousfirstvisibleitem; mpreviousvisibleitemcount = (mpreviousvisibleitemcount == -1) ? mpreviousvisibleitemcount : mpreviousvisibleitemcount; checkandhandlefirstvisiblecellchange(); checkandhandlelastvisiblecellchange(); mpreviousvisibleitemcount = mcurrentfirstvisibleitem; mpreviousvisibleitemcount = mcurrentvisibleitemcount; } private void isscrollcompleted() { if (mcurrentvisibleitemcount > 0 && mcurrentscrollstate == scroll_state_idle) { if (mcellismobile && mismobilescrolling) { handlemobilecellscroll(); } else if (miswaitingforscrollfinish) { toucheventsended(); } } } public void checkandhandlefirstvisiblecellchange() { if (mcurrentfirstvisibleitem != mpreviousfirstvisibleitem) { if (mcellismobile && mmobileitemid != invalid_id) { updateneighbourviewsforid(mmobileitemid); handlecellswitch(); } } } public void checkandhandlelastvisiblecellchange() { int currentlastvisibleitem = mcurrentfirstvisibleitem + mcurrentvisibleitemcount; int previousvisiblelastitem = mpreviousfirstvisibleitem + mpreviousvisibleitemcount; if (currentlastvisibleitem != previousvisiblelastitem) { if (mcellismobile && mmobileitemid != invalid_id) { updateneighbourviewsforid(mmobileitemid); handlecellswitch(); } } } };
}
in init method try cast onitemlongclicklistener
onitemclicklistener
. click , long click different. try setonitemlongclicklistener
instead.
Comments
Post a Comment