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

Ahm*_*ali 16 android listview scroll autoscroll

我一直在研究一个ListView想法,它在没有用户交互的情况下自动滚动,并且使用android API(例如smoothScrollToPositionFromTop)绝对可行.

我已经实现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();
Run Code Online (Sandbox Code Playgroud)

小智 15

我建议,你的适配器以有效的方式实现.所以这段代码只是滚动列表视图

你需要尝试另一个变量值

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();
                        }
                    });
Run Code Online (Sandbox Code Playgroud)

  • 毫无疑问,你应得的赏金,但是我的无限适配器中有一个关于`ListView`的问题,当列表视图滚动时,看起来`getView(...)`没有被调用,所以我看到空单元格之后最后一个单元格滚动,滚动时是否需要使`ListView`无效? (3认同)
  • @Ahmad Kayyali只需将listView.scrollBy(0,heightToScroll)更改为listView.scrollListBy(heightToScroll),它就可以使用ListView. (3认同)