【问题标题】:Modifying ListView's smoothScrollToPosition transition修改 ListView 的 smoothScrollToPosition 过渡
【发布时间】:2015-06-26 04:11:07
【问题描述】:

ListView在调用smoothScrollToPosition时的默认行为,它以线速度移动到指定位置。

深入研究 ListView 和 AbsListView 的代码,我可以看到这种行为的发生是因为 AbsListView 使用了一个 PositionScroller 对象(实现 AbsPositionScroller),而该对象又使用了一个 FlingRunnable 对象,在该对象上使用 linear = true 调用 startScroll 方法(最终使其 OverScroller 对象使用 LinearInterpolator)。

我想修改此行为,并让它使用 OverScroller 类默认使用的 Scroller.ViscousFluidInterpolator 类,但我没有找到方法。

我看到 AbsListView 定义了一个 AbsPosScroller 接口(他自己用 PositionScroller 类实现),我可以尝试用我自己的类来实现它,最终使用 ViscousFluidInterpolator,但由于某种原因,这个接口是私有的打包android.widget...

我是否遗漏了某些内容,或者它是否看起来像是以某种方式编写的,以防止它具有类似的行为被自定义?他们为什么要首先编写一个 AbsPosScroller 界面?

关于如何在不必从头开始编写整个 ListView 类的情况下获得我想要的行为的任何线索?

【问题讨论】:

    标签: java android listview scroll customization


    【解决方案1】:

    虽然我仍然不知道他们为什么要编写这些组件以使其行为无法轻松自定义,而实际上这样做很容易,但我提出了 smoothScrollToPosition 的替代实现( awesomeScrollToPosition 在下面的代码中)可以满足我的需要。

    此解决方案使用 OverScroller 对象(内部使用 ViscousInterpolator,除非指定了不同的对象)来提供我正在寻找的效果,用于滚动到可见页面中的元素(实现跨页面滚动的解决方案更加复杂,但这适用于我需要解决的问题。

    我基本上实现了我自己的 ListView 子类 (MyListView) 私有的 Runnable 类,它处理滚动动画,只要动画需要运行,就将自身重新发布到 UI 线程,在每一帧中使用 scrollingListBy(这个方法仅在 KitKat [19] 之后才可用)。

    public class MyListView extends ListView {
    
        private MyScroller mScroller;
    
        /* MyListView constructors here */
    
        public void awesomeScrollToPosition(int position, int duration) {
    
            if (getChildCount() == 0) {
                // Can't scroll without children (visible list items)
                return;
            }
    
            if (mScroller == null) {
                mScroller = new MyScroller();
            }
    
            if (mScroller.isRunning()) {
                mScroller.stop();
            }
    
            int firstPos = getFirstVisiblePosition();
            int lastPos = getLastVisiblePosition();
    
            if (!(firstPos <= position && position <= lastPos)) {
                // Can't scroll to an item outside of the visible range this easily
                return;
            }
    
            int targetPosition = position - firstPos;
            int targetTop = getChildAt(targetPosition).getTop();
    
            mScroller.start(targetTop, duration);
        }
    
        private class MyScroller implements Runnable {
    
            OverScroller mScroller;
    
            boolean mRunning;
            int mLastY;
    
            MyScroller() {
                mScroller = new OverScroller(getContext());
                mRunning = false;
            }
    
            void start(int y, int duration) {
    
                // start scrolling
                mLastY = 0;
                mScroller.startScroll(0, 0, 0, y, duration);
    
                mRunning = true;
                postOnAnimation(this);
            }
    
            boolean isRunning() {
               return mRunning;
            }
    
            @Override
            public void run() {
    
                boolean more = mScroller.computeScrollOffset();
                final int currentY = mScroller.getCurrY();
    
                // actual scrolling
                scrollListBy(currentY - mLastY);
    
                if (more) {
                    mLastY = currentY;
    
                    // schedule next run
                    postOnAnimation(this);
                } else {
                    stop();
                }
            }
    
            public void stop() {
    
                mRunning = false;
                removeCallbacks(this);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      相关资源
      最近更新 更多