Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite Auto Scroll ListView with Scroll Speed Controlled

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();
like image 846
Ahmad Kayyali Avatar asked Dec 19 '12 12:12

Ahmad Kayyali


1 Answers

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();
                        }
                    });
like image 148
Berezovskyi Avatar answered Oct 15 '22 12:10

Berezovskyi