Анимация Android listview слишком быстрая

У меня есть собственный ListView, который показывает элементы «страница за страницей». Итак, я написал метод OnTouch, и он отлично работает, теперь я хочу написать метод OnFling, который будет реализовывать плавную и инерционную прокрутку моего ListView.

Проблема в том, что анимация прокрутки smoothScrollToPosition(int position) не плавная, а очень быстрая. SmoothScrollToPosition (позиция int, смещение int, продолжительность int) работает, но мой 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" 
     />

person anil    schedule 21.04.2014    source источник


Ответы (1)


Решено! Мое решение:

    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 the listview from overshooting.
            smoothScrollToPosition(activeItem + 1);
        }
    }, 
    DURATION * shift);

Возможно, это не лучшее решение, но оно работает!

person anil    schedule 22.04.2014