解决方法GridView.scrollTo()?解决方法、GridView、scrollTo

2023-09-06 04:21:49 作者:弦断心凉、繁华逝

正如这里,Android的GridView.scrollTo()不工作。该解决方案中提到的方法, setSelectedPosition ,似乎并没有在的 GridView的

As mentioned here, Android's GridView.scrollTo() doesn't work. The method the solution mentioned, setSelectedPosition, doesn't seem to exist in GridView

smoothScrollToPosition 没有工作,但我真的不想动画。

smoothScrollToPosition does work, but I really don't want the animation.

有关背景下,我有一个的CursorAdapter -backed 的GridView ,我想以重置,即滚动到顶部,当我改变光标。

For context, I have a CursorAdapter-backed GridView, and I want the view to "reset", i.e. scroll to the top, when I change the cursor.

推荐答案

我一直在使用 setSelection(INT位置)对于这一点,它似乎工作得精细。要滚动到顶部,只需要用0的位置。

I've been using setSelection(int position) for this, and it seems to be working just fine. To scroll to the top, just use 0 for position.

从文档:

如果在触摸模式中,项目不会被选择,但它仍然会被适当地定位。

If in touch mode, the item will not be selected but it will still be positioned appropriately.

编辑: 后加code setSelection 作为一个Runnable:

Added code to post setSelection as a Runnable:

albumsView.post(new Runnable() {
    @Override
    public void run() {
        albumsView.setSelection(0);
    }
});