Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: StaggeredGridLayoutManager scroll to top while initializing

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?

like image 719
Neo Avatar asked Mar 07 '16 12:03

Neo


1 Answers

I solved similar problem before and here is method:

mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @SuppressWarnings("deprecation")
                    @Override
                    public void onGlobalLayout() {
                        ViewTreeObserver observer = mRecyclerView.getViewTreeObserver();
                        scrollRecyclerViewToTop();
                        if (!observer.isAlive()) {
                            return;
                        }
                        if (mScrolledByUser) {
                            if (hasJellyBeanApi()) {
                                observer.removeOnGlobalLayoutListener(this);
                            } else {
                                observer.removeGlobalOnLayoutListener(this);
                            }
                        }
                    }
                });

And:

mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mScrollByUser = true;
        return false;
    }
});

And:

private void scrollRecyclerViewToTop() {
    mRecyclerView.scrollToPosition(0);
    mRecyclerView.scrollBy(0, Integer.MIN_VALUE);
}

The meaning is easy: let RecyclerView always scroll to top until user touches it. Hope this can help you.

like image 120
ywwynm Avatar answered Sep 20 '22 23:09

ywwynm