Infinite Auto Scroll ListView with Scroll Speed Controlled

后端 未结 2 924
北恋
北恋 2020-12-25 08:19

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 i

相关标签:
2条回答
  • 2020-12-25 08:37

    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();
                            }
                        });
    
    0 讨论(0)
  • 2020-12-25 08:55

    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.
    0 讨论(0)
提交回复
热议问题