Infinite Auto Scroll ListView with Scroll Speed Controlled

痞子三分冷 提交于 2019-11-28 21:21:34

问题


I have been working on a ListViewidea where it keeps scrolling automatically with no user interaction and that is absolutely doable using the android APIs for instance smoothScrollToPositionFromTop.

I have implemented ListView BaseAdapter where it load items forever (almost) to get a non stopping self repeated ListView.

What I want to achieve here is to keep myListViewscrolling forever with certain speed (slow) to make items clear and readable while scrolling down, I not sure yet if ListView is my best choice here.

below is a snippet of what I am trying to do. the result is good somehow but it's not smooth enough, I can feel the ListView flickers.

I need to improve smoothness, efficiency and control the speed

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();

回答1:


I suggest, thath your adapter implemented in effective way. so this code is just scrolls listview

you need to try another values of variables

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();
                        }
                    });



回答2:


Here a few pointers : Simulate onFling() programmatically instead of detecting it (Android)

and Programmatically Fling ListView Android

It's hard to figure out what you call smooth enough in your case. Usually smoothness problems are related to a non optimal usage of listviews and troubles in either cell layouts or view creation / recycling inside the getView method of adapters.

Do you use a placeholder ? An important thing to consider is also Drawables usage.

I never achieved what you are looking for, but a simple idea that comes to mind is :

  • find a way to scroll the view of 1 position or 2.
  • use a ring buffer inside your adapter. For instance let's say you got 100 items in your list of items. Then at the beginning, item 0 of the listview is item 0 of your list. When listview is scrolled up of 1 item, then item 0 of listview should become item 1 in your list. Thus the problem would not be scrolling but more syncing with scrolling and displaying an endless list of items.


来源:https://stackoverflow.com/questions/13952357/infinite-auto-scroll-listview-with-scroll-speed-controlled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!