Question

I'm using https://github.com/etsy/AndroidStaggeredGrid/ in my project. Everything works fine, except I can't scroll it to specific position/item at will.

What I've tried so far (everything I could think of, enclosed in one piece of code for you to look at):

final StaggeredGridView gv = (StaggeredGridView) ma.findViewById(R.id.photosList);
gv.setChoiceMode(StaggeredGridView.CHOICE_MODE_SINGLE);
gv.setFocusable(true);
gv.clearFocus();
gv.post( new Runnable() {
    @Override
    public void run() {

        // #1 approach
        gv.requestFocusFromTouch();
        gv.smoothScrollToPosition(index);

        // #2 approach
        gv.setSelection(index);

        View v = gv.getChildAt(index);
        if (v != null) {
            // #3 approach
            v.requestFocus();
        }
        //v.requestFocusFromTouch();
        Utils.log("scroll to " + index);
        gv.requestLayout();
    }
});

Nothing seems to work and I'm begining to think this view has some kind of bug related to my issue. Is there someone who can help me in the matter?

Was it helpful?

Solution

I found that I wasn't able to scroll to a specific location, however, I did find that I was able to restore the state of the gridview. In my application I had to implement a custom back stack mechanism, and you can pass the state which you can get from:

Parcelable state = mgridView.onSaveInstanceState();

The state is a Parcelable, so i was able to putParcelable in an intent, and then retrieve it and restore the state as needed, like so

if(state != null) {
    mgridView.onRestoreInstanceState(state);
}

Upon restoring the state, it restores the previous position in the grid exactly.

Hope this helps anyone else facing this issue!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top