android listview fling animation is too fast -
i have custom listview shows items "page page". i've written ontouch method , works perfect, want write onfling method realize smooth , inertial scrolling of listview.
the problem scrolling animation of smoothscrolltoposition(int position) isn't smooth, it's fast. smoothscrolltoposition(int position, int offset, int duration) works, minsdk must 8 , besides functions place current element badly despite on offset.
this code of onfling method:
@override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { if(null == getadapter()) return false; boolean returnvalue = false; float pty1 = 0, pty2 = 0; if (e1 == null || e2 == null) return false; pty1 = e1.gety(); pty2 = e2.gety(); if (pty1 - pty2 > swipemindistance && math.abs(velocityy) > swipethresholdvelocity) { float currentvelocity = math.min(math.abs(velocityy), math.abs(swipemaxvelocity)); final int shift = (int) ((currentvelocity / swipemaxvelocity) * swipemaxelements + 1); if (activeitem < getadapter().getcount() - shift - 1) activeitem = activeitem + shift; else { activeitem = getadapter().getcount() - 1; return false; } returnvalue = true; } else if (pty2 - pty1 > swipemindistance && math.abs(velocityy) > swipethresholdvelocity) { float currentvelocity = math.min(math.abs(velocityy), math.abs(swipemaxvelocity)); final int shift = (int) ((currentvelocity / swipemaxvelocity) * swipemaxelements + 1); if (activeitem >= shift) activeitem = activeitem - shift; else { activeitem = 0; return false; } returnvalue = true; } smoothscrolltoposition(activeitem); return returnvalue; }
the xml content is:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_main" android:layout_width="match_parent" android:layout_height="match_parent"> <konteh.example.errortest.cardslistview android:id="@+id/cardslistview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:smoothscrollbar="true" />
solved! solution is:
int pixelcount = height * shift * (isforward ? 1 : -1); // calculate approximately shift in pixels smoothscrollby(pixelcount, 2 * duration * shift);//this smooth scroll works! postdelayed(new runnable() { public void run() { smoothscrollby(0, 0); // stops listview overshooting. smoothscrolltoposition(activeitem + 1); } }, duration * shift);
maybe it's not best solution, works!
Comments
Post a Comment