5

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

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

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.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143

2 Answers2

23

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.

From the docs:

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

Edit: Added code to post setSelection as a Runnable:

albumsView.post(new Runnable() {
    @Override
    public void run() {
        albumsView.setSelection(0);
    }
});
Geobits
  • 22,218
  • 6
  • 59
  • 103
  • I've tried that, no luck. Are you using a CursorAdapter? Thanks anyway. – gatoatigrado Oct 14 '12 at 21:52
  • No, just a BaseAdapter extension. Are you trying it before or after you change cursors? I'm not sure it makes a difference, but then I can't see why using a CursorAdapter rather than anything else would make a difference when simply scrolling. – Geobits Oct 14 '12 at 22:00
  • Good call, thank you sooo much!! I was monkeying around with this for a long time. (literally, hours and hours). Apparently there was a problem when both calling setSelection() and changeCursor(). The order of setSelection() and changeCursor doesn't matter (both fail if just called directly in order), but it does work to post back setSelection() [ http://pastebin.com/XmM5jqD3 ]. Please update your answer with that paste :) – gatoatigrado Oct 14 '12 at 22:09
  • Also, I guess I need to wait 22 hours to award the bounty ... will do so after that time elapses. – gatoatigrado Oct 14 '12 at 22:10
  • If you're having problems with setSelection, the problem almost certainly lies in the gridview layout being in the process of being refreshed. Hence, the call to setSelection is ignored. To mitigate this problem use post (which listens for the completion the GridView layout) like this: yourGridView.post(new Runnable() { @Override public void run() { yourGridView.setSelection(0); } }); – ONE Jul 08 '21 at 16:51
1

I have found that in order to reliably use gridView.setSelection(index), I have to first call gridView.setAdapter() (even if the adapter has already been set and has not changed). Like so:

gridView.setAdapter(gridAdapter);
gridView.setSelection(index);
mikejonesguy
  • 9,779
  • 2
  • 35
  • 49
  • Calling setAdapter will always result in the appearance of scrolling to top as the existing adapter is yanked away and replace with a new one. This os bad for performance and one should try to manipulate the existing adapter. – slott Sep 26 '13 at 06:33
  • 1
    Wrong @slott. If the adapter is already set, this does not result in a reset. I didn't say anything about creating a new adapter or removing the old one. All I said was that, in my experience, I have to re-call setAdapter *even if the adapter has already been set* in order for setSelection to be work reliably. – mikejonesguy Oct 01 '13 at 04:07
  • I tried the other approaches here and this is the only one that worked. I was setting a filter on the adapter and wanted the gridview to return to the top. Although the data reset properly, the image sometimes showed the wrong photo in the imageview. Thanks @mikejonesguy – bkurzius Apr 30 '15 at 16:13