Android Java Listview滚动自动慢

时间:2018-11-16 07:02:25

标签: java android listview android-listview

我有一个包含某些元素的列表视图,我必须使此列表自动滚动并缓慢滚动。

当他到达最后一个元素时,他停了下来。

如果在队列中添加了新项目,则列表视图必须重新启动,直到再次到达最后一个元素为止。

有什么建议吗?

3 个答案:

答案 0 :(得分:0)

我建议,以为您的适配器有效地实现了。所以这段代码只是滚动列表视图

您需要尝试其他变量值

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

答案 1 :(得分:0)

使用以下方法滚动您的列表视图。例如:

scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);

方法:

private void scrollMyTableView(View v,final int var, int fromY, int toY, int delay){

    ObjectAnimator objectAnimator = ObjectAnimator.ofInt(v, "scrollY", fromY, toY).setDuration(2000);
    objectAnimator.setStartDelay(delay);
    objectAnimator.start();
    objectAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {}

        @Override
        public void onAnimationEnd(Animator animation) {

        }

        @Override
        public void onAnimationCancel(Animator animation) {}

        @Override
        public void onAnimationRepeat(Animator animation) {}
    });
}

如果新项添加到适配器,并且需要再次转到列表视图的顶部并再次回到底部,则可以使用以下代码执行此操作:

myAdapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                activityListview.setSelection(0);
scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);
            }
        });

要使用以下方法计算列表视图的高度

public int getTotalHeightofListView(ListView listView) {

        ListAdapter mAdapter = listView.getAdapter();

        int totalHeight = 0;

        for (int i = 0; i < mAdapter.getCount(); i++) {
            View mView = mAdapter.getView(i, null, listView);

            mView.measure(
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),

                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

            totalHeight += mView.getMeasuredHeight();

        }
        return totalHeight;

    }

答案 2 :(得分:0)

使用列表视图smoothScrollToPosition(int position)函数。

也请看看这篇文章 https://www.codota.com/code/java/methods/android.widget.ListView/smoothScrollToPosition

相关问题