Android: StaggeredGridLayoutManager scroll to top while initializing

Viewed 2490

I'm using StaggeredGridLayoutManager for my image gallery. I've set setReverseLayout(true), so that images get stacked from bottom.

The problem I'm facing is that at initialization of app, the scroll points at bottom of gallery. I would want the scroll to be at the top of the gallery when user first starts the app.

I've tried using scrollToPosition (the following code snippet) but then the scroll ends up somewhere in middle of gallery, probably because the images haven't loaded up properly at the time of calling scrollToPosition.

mLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
    mLayoutManager.setReverseLayout(true);

mRecyclerView.setLayoutManager(mLayoutManager);
mImageList = ((MainActivity)getActivity()).getImages();
adapter = new ImageAdapter(getActivity(),mImageList);
mRecyclerView.setAdapter(adapter);
mRecyclerView.scrollToPosition(mImageList.size()-1);

Is there a proper way to point the scroll at the top, rather than at the bottom?

5 Answers
Related