具有控制滚动速度的无限自动滚动列表视图



我一直在研究 ListView的想法,它在没有用户互动的情况下自动滚动,并且使用android apis可以做到绝对可行。

我已经实现了ListView BaseAdapter,它永远加载项目(几乎)以获取不停止自我重复的ListView

我想在这里实现的目标是保持我的ListView以一定的速度(慢)永远滚动以使物品在向下滚动时清晰可读,我不确定ListView是否是我的最佳选择。

下面是我要做的事情的片段。结果很好,但是它不够平滑,我可以感觉到listview闪烁。

我需要改善平滑度,效率并控制速度

new Thread(new Runnable() {
    @Override
    public void run() {
        int listViewSize = mListView.getAdapter().getCount();
        for (int index = 0; index < listViewSize ; index++) {
            mListView.smoothScrollToPositionFromTop(mListViewA.getLastVisiblePosition() + 100, 0, 6000);
            try {
                // it helps scrolling to stay smooth as possible (by experiment)
                Thread.sleep(60);
            } catch (InterruptedException e) {
            }
        }
    }
}).start();

我建议,您的适配器以有效的方式实现。因此,此代码只是滚动listView

您需要尝试其他变量的值

final long totalScrollTime = Long.MAX_VALUE; //total scroll time. I think that 300 000 000 years is close enouth to infinity. if not enought you can restart timer in onFinish()
final int scrollPeriod = 20; // every 20 ms scoll will happened. smaller values for smoother
final int heightToScroll = 20; // will be scrolled to 20 px every time. smaller values for smoother scrolling
listView.post(new Runnable() {
                        @Override
                        public void run() {
                                new CountDownTimer(totalScrollTime, scrollPeriod ) {
                                    public void onTick(long millisUntilFinished) {
                                        listView.scrollBy(0, heightToScroll);
                                    }
                                public void onFinish() {
                                    //you can add code for restarting timer here
                                }
                            }.start();
                        }
                    });

在这里有几个指针:仿真onfling()编程而不是检测(android)

和编程式的listView android

在您的情况下,很难弄清楚您所说的足够光滑。通常,平滑度问题与列表视图的非最佳用法和麻烦中的cell view和"查看创建/回收"的方法有关。

您使用占位符吗?要考虑的一个重要的事情也是可绘制的用法。

我从来没有达到过您想要的东西,但是想到的一个简单的想法是:

  • 找到一种滚动1位置或2位的视图的方法。
  • 在适配器内使用环缓冲区。例如,假设您的项目列表中有100个项目。然后在开始时,列表视图的项目0是您列表的项目0。当ListView滚动为1个项目时,ListView的项目0应该成为列表中的项目1。因此,问题不会滚动,而是与滚动和显示无尽的项目列表更加同步。

最新更新