简体   繁体   English

Android ListView滚动到位置

[英]Android ListView scrolling to position

private void setListviewSelection(final ListView list, final int pos) {
    list.post(new Runnable() {
        @Override
        public void run() {

            list.setSelection(pos);

            for (int i = 0; i < list.getChildCount(); i++)  {
                    View v = list.getChildAt(i);

                    if (i == pos && v != null)
                        v.setBackgroundColor(Color.argb(200, 51, 181, 229));
                    else if (v != null)
                        v.setBackgroundColor(Color.BLACK);
            }
        }
    });
}

Here is a code I'm using to imitate a selection in my music player. 这是我用来模仿音乐播放器中的选择的代码。 The idea is that when user press Next or Previous button an element is highlighted in the ListView , but this is not working the way I want, because setSelection doesn't scroll smoothly and basically some elements are not highlighted correctly. 这个想法是,当用户按下Next或Previous按钮时, ListView中的一个元素被突出显示,但是这不能按照我想要的方式工作,因为setSelection不能平滑滚动,并且基本上某些元素没有被正确突出显示。 For better explanation, what I'm actually trying to implement is a Winamp app which has that way of scrolling when you press next/previous button (when viewing your playlist). 为了获得更好的解释,我实际上试图实现的是Winamp应用程序,该应用程序具有在您按下下一个/上一个按钮时(在查看播放列表时)滚动的方式。

Using setSelectionFromTop() , smoothScrollToPosition() didn't work correctly either. 使用setSelectionFromTop()smoothScrollToPosition()也不正确。

You can only update Views from the main thread. 您只能从主线程更新Views Move your code to the Main thread. 将您的代码移到主线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM