Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

workarounds for GridView.scrollTo()?

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.

like image 423
gatoatigrado Avatar asked Oct 12 '12 04:10

gatoatigrado


2 Answers

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);
    }
});
like image 185
Geobits Avatar answered Oct 31 '22 17:10

Geobits


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);
like image 20
mikejonesguy Avatar answered Oct 31 '22 17:10

mikejonesguy