1
我有一個自定義的ListView,顯示項目「逐頁」。所以我寫了OnTouch方法,它的工作原理非常完美,現在我想寫一個OnFling方法,它可以實現ListView的平滑和慣性滾動。Android的ListView一擲動畫太快
問題是smoothScrollToPosition(int position)的滾動動畫不平滑,速度非常快。 smoothScrollToPosition(int position,int offset,int duration)有效,但是我的minSdk必須是8,而且這個函數不管偏移量如何都會嚴重地放置當前元素。
這是我OnFling方法的代碼:
@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;
}
XML內容是:
<?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"
/>