【问题标题】:How to perform animation listview scroll or limit the speed of scroll如何执行动画列表视图滚动或限制滚动速度
【发布时间】:2016-03-21 07:43:00
【问题描述】:

如何在使用动画自动停止滚动时将项目从屏幕上移开。目前它适用于以下代码,但滚动执行速度非常快。

所以我的要求是使用动画来执行此操作,以便用户可以看到列表视图的滑动。

下面是我的代码,但我没有得到预期的结果。

listview.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE) {
                    View child = layout.getChildAt(0);    // first visible child
                    Rect r = new Rect(0, 0, child.getWidth(), child.getHeight());     // set this initially, as required by the docs
                    double height = child.getHeight() * 1.0;
                    layout.getChildVisibleRect(child, r, null);
                    if (Math.abs(r.height()) != height)
                    {
                        //only smooth scroll when not scroll to correct position
                        if (Math.abs(r.height()) < height / 2.0)
                        {
                            listview.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    listview.smoothScrollBy(listview.getFirstVisiblePosition(), 10000);
                                }
                            }, 500);
                        } else if (Math.abs(r.height()) > height / 2.0)
                        {
                            listview.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    listview.smoothScrollBy(listview.getFirstVisiblePosition(), 10000);
                                }
                            }, 500);
                        } else
                        {

                            listview.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    listview.smoothScrollBy(listview.getFirstVisiblePosition(),10000);
                                }
                            },500);

                        }

                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

【问题讨论】:

标签: java android listview android-studio


【解决方案1】:

老问题,但仅供参考,这对我有用:

    if (condition) {
        // add stuff to begining of ListView and scroll up
        arrayList.add(0, stuff);
        listView.post(new Runnable() {
            @Override
            public void run() {
                listView.smoothScrollToPosition(0);
            }
        });
    } else {
        // add stuff to end of ListView and scroll down
        arrayList.add(index, stuff);
        listView.post(new Runnable() {
            @Override
            public void run() {
                listView.smoothScrollToPosition(adapter.getCount() - 1);
            }
        });
    }

基本上只是smoothScrollToPosition 而不是smoothScrollBy

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多